-1

I'm learning Javascript from Codecademy and I was working on the extra problems to see if I could do them. I'm currently working on the rock paper scissors game and I was trying to have it pull up an error if the user put in an option other than rock, paper or scissors. I managed to get it to pull up the error message, but the script will keep running and the computer will still choose one of the three.

How would I get the program to stop if the user types in something other than rock paper or scissors and only return the error message instead of returning the message then continuing to display the computer's choice?

var userChoice = prompt("Do you choose rock, paper or scissors?");

if  (userChoice !== "rock" || "scissors" || "paper") {
    console.log("That is not one of the options");
}

var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
} console.log("Computer: " + computerChoice);


var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins";
        } else {
            return "paper wins";
        }
    }
    else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins";
        } else {
            return "scissors wins";
        }
    }
    else if (choice1 === "scissors") {
        if (choice2 === "paper") {
            return "scissors wins";
        } else {
            return "rock wins";
        }
    }
}

compare(userChoice, computerChoice)
Ryan
  • 25
  • 4

4 Answers4

3

Method 1

if  (userChoice !== "rock" && userChoice  !== "scissors" && userChoice  !== "paper") {
    console.log("That is not one of the options");
    return false;
}

Method 2

switch(userChoice)
{
  case 'rock': break;
  case 'scissors': break;
  case 'paper': break;
  default: {
     console.log(userChoice + "That is not one of the options");
     return false;
  }
 }

working here at jsfiddle: enter link description here

Community
  • 1
  • 1
Fals
  • 6,813
  • 4
  • 23
  • 43
0

Put it into a function and return in case of error.

var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins";
        } else {
            return "paper wins";
        }
    }
    else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins";
        } else {
            return "scissors wins";
        }
    }
    else if (choice1 === "scissors") {
        if (choice2 === "paper") {
            return "scissors wins";
        } else {
            return "rock wins";
        }
    }
}

var game = function() {
    var userChoice = prompt("Do you choose rock, paper or scissors?");

    if  (userChoice !== "rock" || "scissors" || "paper") {
        console.log("That is not one of the options");
        return;
    }

    var computerChoice = Math.random();
    if (computerChoice < 0.34) {
        computerChoice = "rock";
    } else if(computerChoice <= 0.67) {
        computerChoice = "paper";
    } else {
        computerChoice = "scissors";
    }
    console.log("Computer: " + computerChoice);
    compare(userChoice, computerChoice);
}

game();
fiskeben
  • 3,395
  • 4
  • 31
  • 35
0

You can use an exception, as seen here: Is it possible to stop JavaScript execution?

Basically, after you trigger the error message, do:

throw new Error("Your Error Message!");

Although that would end the whole game, which isn't advisable for a typo on the user's part. It would effectively stop the program.

Community
  • 1
  • 1
TheInnerParty
  • 396
  • 2
  • 13
  • I'd go with @Fals's answer and then ask the question again until the user gets it right. – TheInnerParty Apr 30 '15 at 20:27
  • When I tried it that way it gave me the message that said it was an illegal return statement. Would it have to be a function in order to do what I'm wanting it to do? Or is there a way to do it with a regular if statement? – Ryan Apr 30 '15 at 20:28
  • Rapjr11 gave me the idea to put it in an else clause that way I wouldn't have to do it with a function. It worked perfectly. Thank you for your help. – Ryan Apr 30 '15 at 20:32
0

If the if statement is inside a function, use return false; at the end of the if statement.

If the if statement is outside a function, wrap the remainder of the code in anelse clause so it will not execute if the initial condition succeeds.

rapjr11
  • 46
  • 5
  • This did it perfectly. I was trying the return false but It kept giving me errors. I realized it was because it was not a function. once I added the else clause it fixed it perfectly. Thank you so much. – Ryan Apr 30 '15 at 20:31