[英]How can I stop a loop without leaving it (no 'break')
For example I have this: 例如我有这个:
for (i = 0; i < wLength; i++) {
if(dummy === word.charAt(i)) {
letters[i] = dummy;
//cs == 0 ? score1++ : score2++
} else {
fail++
break;
}
}
It will increment 'fail'
as many times as the 'wLength'
, but I just want it to increment once each iteration (if 'dummy'
is false) and without using break (because it will leave the entire loop). 它会增加
'fail'
到'wLength'
,但是我只希望它每次迭代增加一次(如果'dummy'
为假)并且不使用break(因为它将离开整个循环)。
You can try with "continue". 您可以尝试“继续”。 It will go to next round of loop without leaving it.
它将不离开而进入下一轮循环。
for(i = 0; i < wLength; i++){
if(dummy === word.charAt(i)) {
letters[i] = dummy;
//cs == 0 ? score1++ : score2++
}
else {
fail++;
continue;
}
}
You can always use "while" loop instead of "for" IE: 您可以始终使用“ while”循环而不是“ for” IE:
var n = 0;
var x = 0;
while (n < 3) {
n++;
x += n;
}
you have an syntax error fail++
(where is semicolon) tha is why it stops after if condition fails 您有语法错误
fail++
(分号在哪里)tha是为什么如果条件失败后它将停止的原因
for(i = 0; i < wLength; i++){
if(dummy === word.charAt(i)) {
letters[i] = dummy;
//cs == 0 ? score1++ : score2++
}
else {
fail++;
}
}
You can use a flag variable to achieve your requirement 您可以使用标志变量来满足您的要求
let flag = true
for (i = 0; i < wLength; i++) {
if(dummy === word.charAt(i)) {
letters[i] = dummy;
//cs == 0 ? score1++ : score2++
}else if (flag) {
flag = false
}
}
if (!flag) fail+=1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.