开发者

For Loops Using Javascript

I'm trying to convert this code from Javascript to CoffeeScript:

for (var i = 0; i < names.length; i开发者_开发问答++) {
    str += "Hello" + names[i] + "!<br />";
}

But at the CoffeeScript project home page there is only a simple example of how to do for loops and I can't understand it quite well too, so how can I make convert that to CoffeeScript?


I would do it like this:

msg = ("Hello #{name}!" for name in names).join '\n'


Try this:

str += 'Hello' + name + '!<br />' for name in names


Šime and Acorn beat me to the best answers, but it's worth adding that the literal translation of your code would be

for i in [0...names.length]
  str += "Hello #{names[i]}!<br />"

or using postfix rather than indentation,

str += "Hello #{names[i]}!<br />" for i in [0...names.length]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜