Here, the output is 9, however I don't see anything related to 9 or how it could give an output of 9. How is it possible?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Here, the output is 9, however I don't see anything related to 9 or how it could give an output of 9. How is it possible??

var x = 6; var y = 0; while(x>0 && y<6) { x--; y += x; } document.write(y);

26th Jun 2019, 5:12 AM
eMBee
eMBee - avatar
3 Answers
+ 4
If we run through the loop, it's easier to understand. x = 6 y = 0 x-- //x = 5 y += x // y = 5 /*next iteration*/ x-- // x = 4 y += x // y = 9 y is bigger than 6, so the loop stops there
26th Jun 2019, 5:23 AM
Airree
Airree - avatar
+ 4
Thanks so much Airree for that vivid explanation
26th Jun 2019, 5:35 AM
eMBee
eMBee - avatar
+ 1
not understand
29th Jun 2019, 4:21 PM
mimi