0
I am super confused?
var num=0; for (i = 0; i <= 10; i++) { if (i == 10) { break; } //document.write(i + "<br />"); num += i; } document.write(num) When I print the (num) the output is 45? If I change i==5 the output is 10?? Am I missing something here?
3 Answers
+ 3
when, i == 5 it will obviously break the loop so the 5 will not be incremented on the num.
That's why the value added on num are
0 + 1 + 2 + 3 + 4 = 10
+ 4
0+1+2+3+4+5+6+7+8+9=45
+ 2
When i =0+1=1
1+2=3
3+3=6
6+4=10
10+5=15
15+6=21
21+7=28
28+8=36
36+9=45