thinkrite
thinkrite

Reputation: 87

Javascript won't exit do while loop

So I'm making a browser based game. I want it to display a prompt which will only move on to the next bit of code if a correct answer is inputted. However despite a correct answer the prompt displays, stuck in an infinite loop.

Here is the code.

do {
var cavalry = prompt("Now you need to deploy your troops. They wait for your command. First how shall you deploy your cavalry? -all left -all right -split evenly -stronger left -stronger right").toLowerCase();

switch(cavalry) {
    case 'all left':
        console.log("Your chose all left.");
        break;

    case 'all right':
        console.log("You chose all right.");
        break;

     case 'split evenly':
        console.log("You chose split evenly.");
        break;

    case 'stronger left':
        console.log("You chose stronger left.");
        break;

    case 'stronger right':
        console.log("You chose stronger right.");
        break;

    default:
        alert("That is not an option");
        console.log("Please choose one of the options.");
        break;
   }

}

while (cavalry!="all right" || cavalry!="all left" || cavalry!="stronger left" || cavalry!="stronger right" || cavalry!="split evenly"); 

Upvotes: 0

Views: 581

Answers (2)

strapro
strapro

Reputation: 163

If I understand correctly you want to break both the switch and the while loop if the user inputs something that is acceptable. You should check out labels

How to break nested loops in javascript?

Upvotes: 0

Iftah
Iftah

Reputation: 9572

well cavalry!="all right" || cavalry!="all left" is always true, since it can't be equal to both.

I think you meant cavalry!="all right" && cavalry!="all left" && ...

Upvotes: 3

Related Questions