Javascript 如何在检测到错误后让我的循环从头开始
我试图让我的循环在遇到用户输入错误时重新启动。我需要从一开始就重新开始,而不仅仅是最后一个问题 下面当它说validImput=false时,这就是我试图让它重新启动的地方Javascript 如何在检测到错误后让我的循环从头开始,javascript,Javascript,我试图让我的循环在遇到用户输入错误时重新启动。我需要从一开始就重新开始,而不仅仅是最后一个问题 下面当它说validImput=false时,这就是我试图让它重新启动的地方 { var validInput = true; var start = confirm('Add item to shoping cart'); if (start == true) { // ask first question var orderProductCodeArr = parseInt(pr
{
var validInput = true;
var start = confirm('Add item to shoping cart');
if (start == true) {
// ask first question
var orderProductCodeArr = parseInt(prompt('Enter input: '), 10);
if (isNaN(orderProductCodeArr)) {
alert("input is not a valid number");
validImput = false
} else if (orderProductCodeArr < 0 || orderProductCodeArr >= PRODUCT_LIST.length) {
alert("code does not match any item");
validInput = false;
}
// ask second question
else if(validInput == true) {
var item = PRODUCT_LIST[orderProductCodeArr];
alert("item is: " + item);
}
// get quantity input
var quanityArr = parseInt (prompt('Enter quality amount'),10);
if (isNaN(quanityArr)) {
alert("input is not a valid number");
validInput = false;
}
} else {
document.writeln('still to come')
}
{
var validInput=真;
var start=confirm(‘将商品添加到购物车’);
if(start==true){
//问第一个问题
var orderProductCodeArr=parseInt(提示('enterinput:'),10);
if(isNaN(orderProductCodeArr)){
警报(“输入不是有效数字”);
validImput=false
}else if(orderProductCodeArr<0 | | orderProductCodeArr>=产品列表.length){
警报(“代码与任何项目不匹配”);
validInput=假;
}
//问第二个问题
else if(validInput==true){
变量项=产品列表[orderProductCodeArr];
警报(“项目为:+项目”);
}
//获取数量输入
var quanityArr=parseInt(提示(‘输入质量金额’),10);
if(isNaN(quanityArr)){
警报(“输入不是有效数字”);
validInput=假;
}
}否则{
document.writeln('仍将出现')
}
}试试看
function test()
{
for(var s=0;s<5;s++)
{
try
{
//body of the for loop
}catch(e){s=0;}
}
}
功能测试()
{
对于(var s=0;s而言,通常的重新开始的方法是某种循环构造,通常使用,而如下所示:
while (true) {
// your loop code here
// you can use break; to break out of the while loop
// anywhere to stop repeating
// you can use continue; to jump to the next iteration immediately
}
var doAgain = true;
while (doAgain) {
// within the loop, you set doAgain to false when you are done
// and don't want to repeat the loop again
}
或者,有时使用如下循环条件:
while (true) {
// your loop code here
// you can use break; to break out of the while loop
// anywhere to stop repeating
// you can use continue; to jump to the next iteration immediately
}
var doAgain = true;
while (doAgain) {
// within the loop, you set doAgain to false when you are done
// and don't want to repeat the loop again
}
你发布了所有的,除了那个循环:)@hanky Panky看看我需要什么帮助lol我想通过把所有东西都放在花括号里,它就成为了循环的一部分。@NidhishKrishnam如果不太麻烦,你能给我解释一下你的代码在做什么吗?在for循环中,如果出现任何异常,它会被捕获,循环会从头开始可能是个愚蠢的问题,而且如果是的话,我很抱歉,函数test(){for(var s=0;sYes)只有一个循环,但是当我这样做时,它阻止了它一起工作,确认框不会出现…所以我做错了。你确定循环中发生了任何异常吗