在 JavaScript 中循环直到满足条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35012382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loop in JavaScript until a condition is met
提问by Funktion
I'm trying to loop indefinitely until a condition is met...is the following correct?
我正在尝试无限循环直到满足条件……以下正确吗?
It seems not to be.
似乎不是。
var set = false;
while(set !== true) {
var check = searchArray(checkResult, number);
if(check === false) {
grid.push(number);
set = true;
}
}
回答by Nina Scholz
回答by MobileX
The code will loop while searchArray result is not falseand until it becomes false. So the code is correct if you wanted to achieve such behavior and it's not correct otherwise.
代码将在 searchArray 结果不为 false时循环,直到它变为false。所以如果你想实现这样的行为,代码是正确的,否则就不正确。
回答by Iblob
Let's go over this. You want the code to loop until the function searcharray() returns true, right?
让我们来看看这个。您希望代码循环直到函数 searcharray() 返回 true,对吗?
First, the code creates the variable "set" and sets it to false.
首先,代码创建变量“set”并将其设置为false。
Then while set is notequal to true (would suggest using triple equals here), run this code:
然后当 set不等于 true (建议在这里使用三重等于)时,运行以下代码:
Create the variable "check" and set it to what searcharray returns.
创建变量“check”并将其设置为 searcharray 返回的值。
If searcharray returns false, it will add a number on to the end of the array grid as a new entry and then set "set" to true.
如果 searcharray 返回 false,它将在数组网格的末尾添加一个数字作为新条目,然后将“set”设置为 true。
Then it loops again. If searcharray returned true, it loops again as set is still false. If search array returned false, it does not loop again and skips to the end.
然后它再次循环。如果 searcharray 返回 true,它会再次循环,因为 set 仍然为 false。如果搜索数组返回false,则不会再次循环并跳到最后。