+ 3

I don't understand why two codes shows different results. Can anyone explain to me ?

I have attached two javascript code. Have a good look. https://code.sololearn.com/WBTs2RdB5FoJ/?ref=app

21st Jul 2018, 2:32 PM
Ugyen Chophel
4 Answers
+ 3
Thanks for your response.
21st Jul 2018, 2:48 PM
Ugyen Chophel
+ 2
thats because the second while loop is exactly the same, but it is only printed after the while loop has finished function(){ while(){} } document.write(); ^this should be inside the while loop to output the same as the first one
21st Jul 2018, 2:46 PM
Roel
Roel - avatar
+ 2
no problem
21st Jul 2018, 2:49 PM
Roel
Roel - avatar
0
The two codes shows different results as in the 1st code the [ document.write(); ] is inside the while loop so it will print the output of each iteration of the while loop.... but in the 2nd code the [ document.write(); ] is outside the while loop so it will print the final output of the while loop.... i hope you understand it..!! to make it the same output place the [ document.write(); ] of the 2nd code inside the while loop, like this: [ //First code document.write("First Code<br/>"); var x=0; while(x<4){ document.write(x+ "<br/>"); x++; } document.write("<hr/>Second Code<br/>"); //Second code var y=0; while(y<4) { document.write(y+ "</br>"); y++; } //document.write(y); ]
21st Jul 2018, 6:40 PM
Shevar Sansiro
Shevar Sansiro - avatar