multiple conditions within a while loop - Javascript

14,043

Solution 1

You could also use an array and indexOf

var userChoice = prompt("rock, paper, scissors, rope?").toLowerCase();
while (["rock", "paper", "scissors", "rope"].indexOf(userChoice) === -1) {
    userChoice = prompt("Please choose: rock, paper, scissors, rope?").toLowerCase();
}

alert("You chose '" + userChoice + "'");

Solution 2

The syntax "rock" || "paper" is valid, but the OR will bind later than the equality check, so basically you write:

while ((userChoice != "rock") || "paper" || "scissors" || "rope") {
    //...
}

Now since you use such or's, you basically do an effectiveness check and at least "paper" is effective. You can however get the behavior you aim, by using explicit checks and using the && operator instead of the ||:

while (userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors" && userChoice !== "rope") {
    //...
}

Solution 3

While userChoice does not equal any of the choices... so AND

while (userChoice != "rock" && userChoice != "paper" && userChoice != "scissors" && userChoice != "rope") {

Solution 4

Maybe an array is smarter to use with Array.prototype.indexOf().

var userChoice;
while (!~["rock", "paper", "scissors", "rope"].indexOf(userChoice)) {
    userChoice = prompt("Sorry invalid input, please enter either: rock, paper,scissors, or rope.");
}

Solution 5

for more complicated rules (such as ignoring case sensitivity), it is possible to use regular expressions, e.g. match:

while( ! userChoice.match(/^\s*(?:rock|paper|scissors|rope)\s*$/i)) { ... }

(see explanation of the regex on https://regex101.com/r/sV3tJ1/1#javascript)

note: thanks Willem Van Onsem for suggesting regex


also avaliable are object literals:

var validChoices = {rock: true,
                    paper: true,
                    scissors: true,
                    rope: true},
    userChoice = ...
while ( ! validChoices.hasOwnProperty(userChoice)) {
  ...
}

or ES6 Set for the newest browsers as of 2016: new Set(["rock", "paper", ...])

Share:
14,043
Admin
Author by

Admin

Updated on June 18, 2022

Comments

  • Admin
    Admin almost 2 years

    I want to prompt the user for an input and if it isn't a valid option reprompt until a valid input is entered.

    Right now it always reprompts not matter the input, correct or not.

    Here is the code:

    var userChoice = prompt("Do you choose rock, paper, scissors or rope?");
    while (userChoice != "rock" || "paper" || "scissors" || "rope") {
        userChoice = prompt("Sorry invalid input, please enter either: rock, paper,scissors, or rope.");
    }
    

    Seems like it should be a simple thing to do, am I misunderstanding how to use while loops? maybe the operators are wrong? haven't been able to find anything useful, any help is appreciated!