Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoffeeScript syntax using multiple variables inside for-loop

What's the equivalent CoffeeScript syntax of the native JS' multiple variables for-loop?

for(var a = 0, b = 0; a < 100; a++, b += 10)
{
  console.log(a, b);
}
like image 834
user587841 Avatar asked Sep 27 '26 03:09

user587841


1 Answers

Instead of a for construct like JavaScript's, CoffeeScript relies on comprehensions and ranges. From the documentation:

The only low-level loop that CoffeeScript provides is the while loop.

You could do something involving a range for a, with b being updated manually:

b = 0
for a in [0..99]
  console.log a b
  b += 10

Or use that while:

a = 0
b = 0
while a < 100
  console.log a b
  ++a
  b += 10
like image 160
T.J. Crowder Avatar answered Sep 28 '26 17:09

T.J. Crowder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!