It seems like a simple conversion, but I can’t seem to find the syntax for it.
i = start
while(if step > 0 then i < end else i > end)
array.push i
i += step
start, end, and step are a signed integers
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You should read the CofeeScript page on loops. But the way to do this in CoffeeScript is with a list comprehension iterating over a range:
But note that a list comprehension returns a value. For instance, given this code:
The variable
arraywinds up with the value[10,8,6], as expected, but sincepushreturns the new length of the array it just pushed onto, the return value of that last statement – which will be returned if it’s the last thing in a function, or printed if you enter the above at the REPL, etc. – is[1, 2, 3].EDIT So it would be better, as noted below, to just construct the array with the list comprehension in the first place:
When building a range, note that
...yields an exclusive range in terms of the right endpoint, while..yields an inclusive one. So[1..5]includes 5 in the list, while[1...5]stops at 4.Also, if you really find yourself needing the flexibility of the C-style
forloop, you can always embed some literal JavaScript in your CoffeeScript by wrapping it in backticks (`…`):