0

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"); 
2
  • 3
    Because while is always true? Commented Sep 18, 2014 at 10:31
  • Because the break is only breaking the switch statement, not the loop. Commented Sep 18, 2014 at 10:35

2 Answers 2

3

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" && ...

Sign up to request clarification or add additional context in comments.

Comments

0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.