简体   繁体   English

Javascript代码陷入无尽的“ do while”循环中

[英]Javascript code stuck in endless “do while” loop

I'm working on a quiz application with Javascript with some form validation. 我正在使用带有某种形式验证的Javascript测验应用程序。 I'm trying to make it so that if the user inputs an answer that isn't an option, the code tells the user to please put in a proper answer and the program loops back to the beginning of the question. 我正在尝试这样做,以便如果用户输入的答案不是选项,则代码会告诉用户请输入正确的答案,然后程序将循环回到问题的开头。 With the way the code is written now, it loops back to the beginning of the question after input is given, even if the input is a selectable option. 通过现在编写代码的方式,即使输入是可选选项,在给出输入后它也会循环回到问题的开头。 I'm assuming I have something wrong with the "do while loop" whether it's formatted incorrectly or something to do with the loops condition is causing the infinite looping.I'm having a hard time figuring out the problem so any help would be appreciated. 我假设“ do while循环”格式不正确,或者与循环条件有关的原因导致了无限循环。我很难弄清楚这个问题,因此将不胜感激。

You need to fix your comparison (don't want question selected to be "a" nor "b'): 您需要修正比较(不希望问题选择为“ a”或“ b”):

let score = 0;
let question1 = "";


do{
    question1 = prompt("In Empire Strikes Back, which one of luke's hand is cut off by Darth Vader? (a) Left (b) Right");
        switch(question1.toLowerCase()){
            case "a":
                alert("Sorry that's incorrect");
                break;
            case "b":
                alert("That's correct!");
                score+=1;
                break;
            default:
                alert("That answer wasn't an option, please select one of the options listed");
        }
}
while(question1 != "a" && question1 != "b");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM