如何跳出函数中的嵌套循环(javascript)



我读过的很多答案都没有函数。但是,我绝对需要在脚本中包含in函数。

var input = require('readline-sync')
var cart = [];
function func2() {
console.log("this is the inner function")
while (true) {
firstinput = input.questionInt('Please Enter your input (1, 2, 3). Press 0 to Go back to outermost loop. You will still be in the program.')
switch (firstinput) {
case 0: 
break; //breaks out of this entire func2()
case 1: 
func3()
break; //breaks, but goes back to firstinput
case 2:
func4() //im not going to define it here, but similar to func3()
func5() //im not going to define it here, but similar to func3()
break;
case 3:
func5() 
func3() //as you can see, the functions are recurring
break;
default:
console.log('Input valid number, please')
continue;
}
}
}
function func3() {
while (true) {
secondinput = input.question("Hi, please pick 1a, 1b, or 1c! You can Press 0 to exit to the outermost function. YOu will still be in the program, though")
switch (secondinput) {
case '0':
break; //breaks out of func3() and func2()
case '1a':
cart.push("1a")
break; //breaks out of func3()
case '1b':
cart.push("1b")
break; //breaks out of func3()
case '1c':
cart.push('1c')
break; //breaks out of func3()
default:
console.log('invalid input')
continue;
}

}
}
function func1() {
while (true) {
func2()
if ((secondinput == 0) || (firstinput == 0)) {
console.log("I see you've pressed 0. Let's loop through again")
}
else {
console.log('Here is what was in your cart: ' + cart)
}
}
}
func1()

我希望这是清楚的!我需要把内部函数和外部函数分开。这很困难,因为如果我改变了函数中变量的值,函数外的值不会改变。

提前谢谢你

我的问题是:当我进入func3(),我如何打破一切,如果我按'0'?

当您想要停止函数时可以返回,或者继续使循环继续。

while (true) {
firstinput = input.questionInt('Please Enter your input (1, 2, 3). Press 0 to Go back to outermost loop. You will still be in the program.')
switch (firstinput) {
case 0: 
return; //breaks out of this entire func2()
case 1: 
func3()
continue; //breaks, but goes back to firstinput
case 2:
func4()
func5() 
continue;
case 3:
func5() 
func3() //as you can see, the functions are recurring
continue;
default:
console.log('Input valid number, please')
continue;
}
}

应该像这里一样使用return/break/continue的组合

// var input = require('readline-sync')
var cart = [];
let firstinput, secondinput;
function func2() {
console.log("this is the inner function")
while (true) {
firstinput = +prompt('Please Enter your input (1, 2, 3). Press 0 to Go back to outermost loop. You will still be in the program.')
console.log({firstinput})
switch (firstinput) {
case 0: 
return; //breaks out of this entire func2()
case 1: 
func3()
break; //breaks, but goes back to firstinput
case 2:
func4() //im not going to define it here, but similar to func3()
func5() //im not going to define it here, but similar to func3()
break;
case 3:
func5() 
func3() //as you can see, the functions are recurring
break;
case 4: // show cart
case -1: // exit everything
return;
default:
console.log('Input valid number, please')
continue;
}
}
}
function func3() {
while (true) {
secondinput = prompt("Hi, please pick 1a, 1b, or 1c! You can Press 0 to exit to the outermost function. YOu will still be in the program, though")
switch (secondinput) {
case '0':
return false; //breaks out of func3()
case '1a':
cart.push("1a")
return true; //breaks out of func3()
case '1b':
cart.push("1b")
return true; //breaks out of func3()
case '1c':
cart.push('1c')
return true; //breaks out of func3()
default:
console.log('invalid input')
continue;
}

}
}
function func1() {
while (true) {
func2()
if ((secondinput == 0) || (firstinput == 0)) {
console.log("I see you've pressed 0. Let's loop through again")
} else if(firstinput === -1) {
return
}
else {
console.log('Here is what was in your cart: ' + cart)
}
}
}
func1()

你也可以使用带标签的break来退出像这样的代码块

let i=0
a: while(true){
++i
switch(i) {
case 4:
break a; // break the while loop
default:
console.log(i);
}
}
// prints
// 1
// 2
// 3

有关标签分隔符的更多信息,请查看

最新更新