Reputation: 32223
How do I write this for loop in CoffeeScript?
for(i = cc.length - 2, i >= 0, i -= 2)
Upvotes: 1
Views: 865
Reputation: 77416
for i in [cc.length - 2..0] by -2
...
Compilation here. The by
keyword isn't very well-known, but it's invaluable.
One caveat: You have to remember to do the range backward (upper..0
). And you cannot iterate through an array backward with this approach:
for i in arr by -1 # infinite loop!
Upvotes: 5