0
Could anyone explain the answer to this question from the “play” section of SoloLearn?
What is the output of this code? var x = 1; for(;x<5;x++) { x+=x; } alert(x); The answer was 7 but I need some help understanding why? Thanks!
2 Answers
+ 9
x+=x is the same as x = x + x.
So,
x = 1
x = 1 + 1
x++ , now x is 3
x = 3 + 3
x++ , x is now 7
x < 5 is now false, loop ends.
+ 2
Thank you!! That makes sense now :)