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));
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}`);
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)));
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);