0
How is the js for loop sum 55 and not 45?
let sum = 0; for (let i = 0; i <= 9; i++, sum += i); console.log(sum);
4 Answers
+ 3
richard honour
If you do like that inside loop then both i++, sum += i will work same time but if you do like this then sum += i will work after checking condition so sum will be only 45.
let sum = 0;
for (let i = 0; i <= 9; i++) {
sum += i;
}
console.log(sum);//45
+ 2
richard honour
Because we are doing sum += i after i++ so when i <= 9 will be true then after i++, sum = 45 + 10 = 55
+ 1
Thanks never thought bout it that way... âş