Javascript 如果实现了switch case(循环中有一个开关),我如何从循环中退出
如果实现了switch case(循环中有一个开关),我如何从循环中退出Javascript 如果实现了switch case(循环中有一个开关),我如何从循环中退出,javascript,Javascript,如果实现了switch case(循环中有一个开关),我如何从循环中退出 函数playInbestPlace(){ console.log(“来自playInbestPlace的你好”) findEmptyarea(); 对于(变量i=0;i
函数playInbestPlace(){
console.log(“来自playInbestPlace的你好”)
findEmptyarea();
对于(变量i=0;i
如果任何情况有效,我希望它被释放。您可以添加一个变量
found
,如果为true,则中断循环:
function playInbestPlace() {
console.log("hello from playInbestPlace ")
findEmptyarea();
for (var i = 0; i < indexOfEmpty.length; i++) {
var elem = indexOfEmpty[i];
var found = false; // initial found is false
switch (elem) {
case 0:
cells[elem].childNodes[0].append("o");
found = true;
break;
case 2:
cells[elem].childNodes[0].append("o");
found = true;
break;
case 4:
cells[elem].childNodes[0].append("o");
found = true;
break;
case 6:
cells[elem].childNodes[0].append("o");
found = true;
break;
case 8:
cells[elem].childNodes[0].append("o");
found = true;
break;
}
if(found) // break out if it's true
break;
}
}
函数playInbestPlace(){
console.log(“来自playInbestPlace的你好”)
findEmptyarea();
对于(变量i=0;i
您可以添加一个找到的变量
,如果该变量为true,则可以中断循环:
function playInbestPlace() {
console.log("hello from playInbestPlace ")
findEmptyarea();
for (var i = 0; i < indexOfEmpty.length; i++) {
var elem = indexOfEmpty[i];
var found = false; // initial found is false
switch (elem) {
case 0:
cells[elem].childNodes[0].append("o");
found = true;
break;
case 2:
cells[elem].childNodes[0].append("o");
found = true;
break;
case 4:
cells[elem].childNodes[0].append("o");
found = true;
break;
case 6:
cells[elem].childNodes[0].append("o");
found = true;
break;
case 8:
cells[elem].childNodes[0].append("o");
found = true;
break;
}
if(found) // break out if it's true
break;
}
}
函数playInbestPlace(){
console.log(“来自playInbestPlace的你好”)
findEmptyarea();
对于(变量i=0;i
当某些条件得到验证时,可以使用标志变量从循环中断
function playInbestPlace() {
console.log("hello from playInbestPlace ");
findEmptyarea();
var keepOnLooping = true;
for (var i = 0; keepOnLooping && i < indexOfEmpty.length; i++) {
if (elem % 2 === 0) {
cells[elem].childNodes[0].append("o");
keepOnLooping = false;
}
}
}
函数playInbestPlace(){
log(“playInbestPlace的你好”);
findEmptyarea();
var keepOnLooping=true;
对于(var i=0;keepOnLooping&&i
我在回答中还添加了epascarello优化。当某些条件得到验证时,您可以使用标志变量中断循环
function playInbestPlace() {
console.log("hello from playInbestPlace ");
findEmptyarea();
var keepOnLooping = true;
for (var i = 0; keepOnLooping && i < indexOfEmpty.length; i++) {
if (elem % 2 === 0) {
cells[elem].childNodes[0].append("o");
keepOnLooping = false;
}
}
}
函数playInbestPlace(){
log(“playInbestPlace的你好”);
findEmptyarea();
var keepOnLooping=true;
对于(var i=0;keepOnLooping&&i
我在回答中还添加了epascarello优化。整个开关
块都是不必要的,所有这些代码都可能是if(elem%2==0){cells[elem].childNodes[0].append(“o”);break;}
整个switch
块都是不必要的,所有这些代码都可能是if(elem%2==0){cells[elem].childNodes[0].append(“o”);break;}
也可以比flagYeah更好地使用break语句,这是一样的。使用标志只是一种避免break
指令并存储退出循环的信息的方法,而无需检查所有数组。您也可以使用break语句比使用标志更好。是的,这是一样的。使用一个标志只是一种避免中断
指令并存储退出循环的信息而不检查所有数组的方法。