Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
How to add two numbers without concatenating them as strings?

The parseInt(sum3) + parseInt(sum5) is not adding the two variables. It just concatenates them. How can I add the two?

I know I can just manually create another variable and put the output of sum3 there and put the output of sum5 in another variable and add them like that but I idk I think that's cheating. So how can I add the sum3 and sum5?

// sum of 3
let max3 = parseInt(1000);
let sum3 = 0;

for ( let three=3; three<=max3; three++ ){
    sum3+=three
}


// sum of 5
let max5 = parseInt(1000);
let sum5 = 0

for (let five=5; five<=max5; five++){
  sum5+=five
}


// sum of 3 and 5

document.write("Sum of 3 = " + sum3 + "<br/>"
   + "Sum of 5 = " + sum5 + "<br/>" + 
    "Sum of 3 and 5 = " + parseInt(sum3) + parseInt(sum5));
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

It's just because you're using + in the context of string concatenation.

Should be:

document.write("Sum of 3 = " + sum3 + "<br/>"
   + "Sum of 5 = " + sum5 + "<br/>" + 
    "Sum of 3 and 5 = " + (sum3 + sum5));

Or you really should use:

document.write(`Sum of 3 = ${sum3} <br/>
        Sum of 5 = ${sum5} <br/>
        Sum of 3 and 5 = ${sum3 + sum5}`);
about 4 years ago · Juan Pablo Isaza Report

0

You can just wrap parseInt(sum3) + parseInt(sum5) in a () bracket and it should do the job.

// sum of 3
let max3 = parseInt(1000);
let sum3 = 0;

for (let three = 3; three <= max3; three++) {
  sum3 += three
}


// sum of 5
let max5 = parseInt(1000);
let sum5 = 0

for (let five = 5; five <= max5; five++) {
  sum5 += five
}


// sum of 3 and 5

document.write("Sum of 3 = " + sum3 + "<br/>" +
  "Sum of 5 = " + sum5 + "<br/>" +
  "Sum of 3 and 5 = " + (parseInt(sum3) + parseInt(sum5)));

about 4 years ago · Juan Pablo Isaza Report

0

You concatenate both string values with the use of '+'.

Don't combine this with string so '+' will still work as expected.

// sum of 3
let max3 = 1000;
let sum3 = 0;

for (let three=3; three<=max3; three++){
    sum3+=three
}


// sum of 5
let max5 = 1000;
let sum5 = 0

for (let five=5; five<=max5; five++){
  sum5+=five
}


// sum of 3 and 5
let totalSum = sum3 + sum5

document.write("Sum of 3 = " + sum3 + "<br/>"
   + "Sum of 5 = " + sum5 + "<br/>" + 
    "Sum of 3 and 5 = " + totalSum);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!