+ 5

javascript conditionals and loops-module 3 quiz-first question

What is the output of this code? var x = 0; while(x<6) { x++; } document.write(x); If i am not wrong the answer should be 5 but it accepts 6 for correct which is not right, right? it would have been if x<=6...

23rd Apr 2017, 8:44 AM
curiouscc
7 odpowiedzi
+ 12
the answer is 6. Run it in the code playground to test. The last time x is tested in the loop it has value 6. 6 < 6 --> false. That's what makes the loop stop.
22nd Apr 2017, 3:16 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 11
The condition 6<6 ends the loop, but the print statement is after the loop.
22nd Apr 2017, 11:18 PM
Ulisses Cruz
Ulisses Cruz - avatar
0
Hi, thank you for your reply. But if 6<6 is false, why would it print it anyway? After a little pondering, I wonder wheter the answer is 6 because last true number is 5 then we have x++ which makes it 6?
22nd Apr 2017, 5:02 PM
curiouscc
0
Ahh! Thank you again! I got it this time :))))
23rd Apr 2017, 8:43 AM
curiouscc
0
To loop over the value of x. Here from 0 to 5. Should print 0, 1, 2 3, 4 ,5 .
24th Apr 2017, 5:58 AM
Alexis Cheuk Yan Lee
Alexis Cheuk Yan Lee - avatar
0
what is the second question?
28th Apr 2019, 7:16 PM
Dimitri Cadau
Dimitri Cadau - avatar
0
6
22nd Aug 2019, 10:30 AM
Md Atif J Khan
Md Atif J Khan - avatar