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!

26th Jun 2019, 11:23 AM
Shaun Williams
Shaun Williams - avatar
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.
26th Jun 2019, 11:28 AM
Jonathan Pizarra
Jonathan Pizarra - avatar
+ 2
Thank you!! That makes sense now :)
26th Jun 2019, 11:40 AM
Shaun Williams
Shaun Williams - avatar