Curtis
Curtis

Reputation: 201

Coffeescript for loop

I am trying to convert some apple chart examples from javascript to coffeescript. Having a tough time trying to figure out how to write this for loop in coffee script. Thanks for any help in advance

for (scale = maxVal; scale >= 0; scale -= stepSize) {...}

Upvotes: 20

Views: 28091

Answers (2)

Tolik Kukul
Tolik Kukul

Reputation: 2016

scale = maxVal
while scale >= 0
  ...
  scale -= stepSize

There's a good tool for converting JS to Coffeescript: http://js2.coffee/

Upvotes: 10

hexid
hexid

Reputation: 3811

This loop will increment by the negative of stepSize.

maxVal = 10
stepSize = 1
for scale in [maxVal..0] by -stepSize
  console.log scale

However, if stepSize is actually 1, then

maxVal = 10
for scale in [maxVal..0]
  console.log scale

would produce the same result

Upvotes: 28

Related Questions