+ 1
Why's this 5 & not 4?
var x = 1, y = 1, z = 0; do { z = x + y; } while (++x <= 1 && y++ >= 1); z += x + y; document.write(z);
3 Answers
+ 6
Personally, I got 7 with it. When in doubt, throw some logs into your code so you can see its flow and variable values to understand how it reached the result.
https://code.sololearn.com/WDMNq1GD9x00/#js
var x = 1, y = 2, z = 0;
document.write("Starting Values: X="+x+" Y="+y+" Z="+z+"<br>");
do {
document.write("Inner Loop Start: "+ z + " = "+x+" + "+y+ "<br>");
z = x + y
document.write("Inner Loop Finish: "+ z + " = "+x+" + "+y+ "<br>");
}
while (++x <= 1 && y++ >= 1);
document.write("After Loop: "+z+" += "+x+" + "+y+ "<br>");
z += x + y;
document.write("End Result: "+z+" += "+x+" + "+y+ "<br>");
::::: OUTPUT ::::::
Starting Values: X=1 Y=2 Z=0
Inner Loop Start: 0 = 1 + 2
Inner Loop Finish: 3 = 1 + 2
After Loop: 3 += 2 + 2
End Result: 7
+ 3
Should actually be 7 as loop is run once!
+ 1
Hey guys, sorry about that.
The question has been modified!
What I'm trying to understand is the condition in the while loop.
Why will the penultimate line of code run seeing that both conditions in the while loop are not true?