跳回if / else语句

时间:2015-03-17 19:40:22

标签: javascript if-statement

我的代码看起来像这样:

var choice1 = prompt("a question").toLowerCase;

if (choice1 === option) {

      alert("Something")
}

else {

      alert("that's not possible")
      go back to the original prompt

}

那么,我将如何回到最初的提示再次提出问题?

2 个答案:

答案 0 :(得分:1)

您可以将整个代码块放在一个函数中,如果没有得到想要的答案,可以再次调用它。

function askQuestion() {
    var choice1 = prompt("a question").toLowerCase;
    if (choice1 === option) {
         alert("Something");
    } else {
         alert("that's not possible");
         //go back to the original prompt
         askQuestion();
    }
}

答案 1 :(得分:0)

做这样的事情:

var flag = false; 

while (!flag) {
    var choice1 = prompt("a question").toLowerCase;

    if (choice1 === option) {    
          alert("Something");
          flag = true; // set flag to true to break out of the loop
    }    
    else {    
          alert("that's not possible")
          // go back to the original prompt    
    }
}
相关问题