Can someone tell me why this code outputs 6 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone tell me why this code outputs 6

var x=3; var i=0; while(i<3){ x+=1; i+=1; }; document.write(x);

22nd Apr 2018, 7:43 PM
Abdullah Bhatti
Abdullah Bhatti - avatar
4 Answers
+ 3
At the beginning x = 3. You go through the loop 3 times and you add 1 to x each times. So finally you have done x = x + 3 = 3 + 3 = 6... Does that help or should I explain another way?
22nd Apr 2018, 7:47 PM
cyk
cyk - avatar
+ 2
Here. Check out this code. Go to the JS tab and read the comments. ☺ https://code.sololearn.com/WO078f1wyUSp/?ref=app
22nd Apr 2018, 8:02 PM
cyk
cyk - avatar
+ 1
Sure ☺
22nd Apr 2018, 7:53 PM
cyk
cyk - avatar
+ 1
ahh thanks man it realy helped😀
22nd Apr 2018, 8:06 PM
Abdullah Bhatti
Abdullah Bhatti - avatar