Sorry for the basic question, but I'm new to coffeescript and the docs aren't making sense to me.
I am referring to the code in this documentation: http://coffeescript.org/#loops Specifically,
# Fine five course dining.
courses = ['greens', 'caviar', 'truffles', 'roast', 'cake']
menu = (i, dish) -> "Menu Item #{i}: #{dish}"
menu i + 1, dish for dish, i in courses
I want courses instead to be pizzas:
pizzas = ["Veggie", "Cheese", "Pepperoni", "Combo"]
menu = (i, pizza) -> "#{i}) #{pizza}"
menu i + 1, pizza for pizza, i in pizzas
And then store the results in a variable, so that the variable contains "1) Veggie 2) Cheese"
, etc. Do I want to do:
pizzas = ["Veggie", "Cheese", "Pepperoni", "Combo"]
menu = (i, pizza) -> "#{i}) #{pizza}"
menuOptions = menu i + 1, pizza for pizza, i in pizzas
I tried running this in one of the script windows, but couldn't get it to work for me and I'm finding the syntax confusing.
I tried running this with console.log menuOptions and got:
4) Combo
Your code works (no syntax errors), but because of the operator precedence, your last line executes like this:
(menuOptions = menu) i + 1, pizza for pizza, i in pizzas
What you want is to wrap your expression in parentheses so the interpreter does what you want:
menuOptions = (menu i + 1, pizza for pizza, i in pizzas)
console.log menuOptions.join(", ")
#=> 1) Veggie, 2) Cheese, 3) Pepperoni, 4) Combo
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments