I know how to do a incrementing for loop in coffeescript such as:
Coffeescript:
for some in something
Generated Javascript:
for (_i = 0, _len = something.length; _i < _len; _i++)
How do I create a decrementing for loop similar to this in Coffeescript?
for (var i = something.length-1; i >= 0; i--)
EDIT: As of CoffeeScript 1.5
by -1syntax is supported.First, you should familiarize yourself with the
bykeyword, which lets you specify a step. Second, you have to understand that the CoffeeScript compiler takes a very naïve approach to loop endpoints (see issue 1187, which Blender linked to), which means thatwill result in an infinite loop—it starts at index 0, increments the index by -1, and then waits until the index hits
something.length. Sigh.So you need to use the range loop syntax instead, which lets you specify those endpoints yourself—but also means you have to grab the loop items yourself:
Obviously that’s pretty messy. So you should strongly consider iterating over
something.reverse()instead. Just remember thatreverse()modifies the array that you call it on! If you want to preserve an array but iterate over it backwards, you should copy it: