for循环中的多个条件 [英] Multiple conditions in for loop

查看:977
本文介绍了for循环中的多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写一个具有多个条件的for循环?



目标Javascript:

  for(k = 1; k< 120&&&myThing.someValue> 1234; k ++){
myThing.action
}

js2coffee.org表示我应该使用while循环:

  k = 1 
,而k < 120和myThing.someValue> 1234
myThing.action()
k ++



有没有办法编写coffescript来编译到我想要的javascript,并在for循环中包含额外的条件?



如果这个问题的答案是false,那么使用coffeescript获得相同功能的最好方法是什么? p>

我最好的while循环解决方案是

  k = 1 
myThing.action()while k ++< 120和myThing.someValue> 1234


解决方案

/ code> loop相当于,而 loop加上几个语句,CoffeeScript仅为 c提供了一种 $ c> loop,使用 while 循环。



当然, 是 always

 `for(k = 1; k <120&& myThing.someValue& k ++){`
do myThing.action
`}`


How can I write a for loop with multiple conditions?

Intended Javascript:

for(k=1; k < 120 && myThing.someValue > 1234; k++){
  myThing.action();
}

js2coffee.org indicates that I should use a while loop:

k = 1
while k < 120 and myThing.someValue > 1234
  myThing.action()
  k++

but this ends up compiling back to a while loop in javascript.

Is there a way to write coffescript to compile to my intended javascript and include extra conditions in the for loop itself?

If the answer to that question is false, then what would be the nicest way to get the same functionality with coffeescript?

My best while loop solution so far is

k = 1
myThing.action() while k++ < 120 and myThing.someValue > 1234

解决方案

Since a for loop is equivalent to a while loop plus a couple of statements, and CoffeeScript offers only one kind of for loop, use a while loop. If you’re trying to get specific JavaScript output, you should probably not be using CoffeeScript.

Of course, there is always

`for(k=1; k < 120 && myThing.someValue > 1234; k++) {`
do myThing.action
`}`

Avoid.

这篇关于for循环中的多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆