-1

I'm working on a quiz application with Javascript with some form validation. 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.

D.Ram
  • 9
  • 2

1 Answers1

2

You need to fix your comparison (don't want question selected to be "a" nor "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");
jh314
  • 27,144
  • 16
  • 62
  • 82
  • wonder if the equivalent `while(!(question1 == "a" || question1 == "b"))` is easier for the OP to understand :p – Jaromanda X Aug 03 '18 at 02:29
  • true dat @ggorlen - but my suggestion was to do with boolean logic - I think some people have trouble when there's more than one NOT in an expression, so I made the expression have only one boolean NOT – Jaromanda X Aug 03 '18 at 02:55
  • I hear you, I'm just jumping on the bandwagon before the `~['a','b'].indexOf(question1)` folks arrive from the other thread. :-) – ggorlen Aug 03 '18 at 02:57