G+_George Kozi Posted December 29, 2016 Share Posted December 29, 2016 Can you wizards help this nice lady? Originally shared by Meg Tufano I'm trying to learn code all by myself (poor me (and a good way to avoid thinking about politics)) and would like to crowdsource the following Javascript question because I don't understand the right answer: Question: What does the alert statement display in the following code: var x = 45; while (x>2) {x- - ;} alert(x); The course says the answer is 2. That makes no sense to me. I think it should be 3. Why is it 2? Or is the course wrong? HELP!!!!!!!!! Link to comment Share on other sites More sharing options...
G+_Fr. Robert Ballecer, SJ Posted December 30, 2016 Share Posted December 30, 2016 Assuming alert(x) prints the value of x, it doesn't get called until after the decrement. So the last time the while is triggered, x=3. It is decremented to 2, then that value is printed. Link to comment Share on other sites More sharing options...
G+_George Kozi Posted December 30, 2016 Author Share Posted December 30, 2016 Fr. Robert Ballecer, SJ thankyou. Meg Tufano Link to comment Share on other sites More sharing options...
Recommended Posts