marijnh / Eloquent-JavaScript

The sources for the Eloquent JavaScript book
https://eloquentjavascript.net
3.03k stars 799 forks source link

Chapter 5, Exercise 'Your Own Loop' #353

Closed ah858 closed 6 years ago

ah858 commented 6 years ago

Each iteration, it [the loop] first runs the test function on the current loop value, and stops if that returns false. Then it calls the body function...

The first run of the loop returns true because the input value of the loop is 3 and the test function is n > 0. If you program the loop to check for a false value the loop never executes.

(I think!) to make the exercise work the body function needs to execute if the test function returns true.

marijnh commented 6 years ago

Yes, that's the idea. The sentence "and stops if that returns false" seems to agree with this—it stops for false, runs for true.