In CoffeeScript, the while
loop comes standard:
while x() y()
However, the following1 doesn't work:
do y() while x()
And this is simply sugar for the first example:
y() while x()
Does CoffeeScript come with a built-in loop that executes at least once?
1As an aside, do
is a keyword — it's used to call anonymous functions.
The while loop is not run because the condition is not met. After the running the for loop the value of variable i is 5, which is greater than three. To fix this you should reassign the value before running the while loop (simply add var i=1; between the for loop and the while loop).
The coffee and cake commands will first look in the current folder to see if CoffeeScript is installed locally, and use that version if so. This allows different versions of CoffeeScript to be installed globally and locally.
The issue with your while loop not closing is because you have an embedded for loop in your code. What happens, is your code will enter the while loop, because while(test) will result in true . Then, your code will enter the for loop. Inside of your for loop, you have the code looping from 1-10.
A while loop evaluates its condition before the first iteration, and inbetween each subsequent iteration. The condition is never evaluated inside the loop body.
The CoffeeScript documentation says:
The only low-level loop that CoffeeScript provides is the while loop.
I don't know of a built-in loop that executes at least once, so I guess the alternative is
loop y() break if x()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With