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?

22nd Dec 2021, 4:12 AM
Ivan
3 Antworten
+ 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
22nd Dec 2021, 5:28 AM
zexu knub
zexu knub - avatar
+ 4
0+1+2+3+4+5+6+7+8+9=45
22nd Dec 2021, 4:19 AM
ChaoticDawg
ChaoticDawg - avatar
+ 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
23rd Dec 2021, 2:11 PM
Atul Mohabe
Atul Mohabe - avatar