How to prompt user to make new choice if they chose something inappropriate?


#1

In line 3 i tried to put an if statement to return "error" if the user chooses something other than rock paper or scissors but there seems to be a syntax error.

var userChoice = prompt("Do you choose rock, paper or scissors?");
if  (userChoice != "rock"||"paper"||"scissors"){
    return"error";}
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 (choice==="scissors"){
                return "rock wins"
            }else{
             return"paper wins"};
        }else if (choice1==="paper"){
            if (choice==="scissors"){
                return "scissors wins"
            }else{
             return"paper wins"};
        }else if (choice1==="scissors"){
            if (choice==="paper"){
                return "scissors wins"
            }else{
             return"rock wins"};
        }
};
console.log(compare(userChoice,computerChoice));

#2

Of course, you have SyntaxError: Illegal return statement because return statement is using in functions. So, you can create function:

function checkUserChoice(choice) {
   if  (choice != "rock" || "paper" || "scissors") {
      return "error";
   }
   return "good";
}

After that we can to call created function:

var choice = checkUserChoice(userChoice);
if (choice === "good") {
   // do something if checkUserChoice return "good";
} else {
   // do something if checkUserChoice return "error"
}

I didn't test my code, but you can try.


#3

first off, you need to compare at all conditions:

if  (userChoice != "rock"|| userChoice != "paper"|| userChoice != "scissors")

@denys.matsevych is right, technically we don't have to do this, but it makes it clearer what you are comparing at each condition, and don't rely on js to interpreter this correctly.

we also need && instead of ||, otherwise it won't work. If the user enters something invalid, all conditions are true, the if statement execute. if the user enters something valid, one condition will be false. but false || true is true, then the if statement won't execute, so we need && because false and true is false (which is good, since the user enters something valid, so else is executed
then we have to build our function:

var validate_input = function(){
   var userChoice = prompt("Do you choose rock, paper or scissors?");
   if  (userChoice != "rock" && userChoice != "paper"&& userChoice != "scissors"){
     return validate_input();
  else {
     return userChoice;
}
}

a loop would be easier, but that hasn't been covered


#4

I agree with you except one. Condition

if  (choice != "rock" && "paper" && "scissors") {...}

is also valid as

if  (userChoice != "rock" && userChoice != "paper"&& userChoice != "scissors") {...}

UPDATED.
What does it mean "def" ?


#5

true, i forget, that is valid as well.

I still find it weird looking tough. i prefer my solution, it is more code, but you are certain of what is compared at each condition


#6

I also prefer 2nd variant. But I didn't find information about def keyword. Could you post a link to doc about def keyword?


#7

oops, that is sloppy. defis the function keyword in python.

good catch :slight_smile:

i need some time to wake up i guess :stuck_out_tongue:


#8

var validateInput = function() { // JS using camelCase notation
   var userChoice = prompt("Do you choose rock, paper or scissors?");
   if (userChoice != "rock" && userChoice != "paper" && userChoice != "scissors"){
     return validate_input(); // missed } before else
  } else {
     return userChoice; // userChoice() is not defined
  }
}

I think, it's just a Monday :slight_smile: We all need to wake up :coffee:


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.