20

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) {...}

2 Answers 2

28

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

Sign up to request clarification or add additional context in comments.

2 Comments

You don't need (0-stepSize), just -stepSize should be sufficient.
use by 1 or by -1 if possible. CoffeeScript (I have version 1.10) creates extremely inefficient (and quite puzzling) code if it must figure this out at runtime.
10
scale = maxVal
while scale >= 0
  ...
  scale -= stepSize

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

1 Comment

Thanks for posting that link. It's helped me out a few times in the last couple days :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.