I really need help with this "rock paper scissors" code. I keep getting "choice1 is undefined


#1

var userChoice = prompt("Do you choose rock, paper or scissors?");
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 === "rock") {
            return "rock wins"
        }
        else {
            return "scissors wins";
        }
        
}

};
var userChoice = choice1;
var computerChoice = choice2;
console.log(userChoice, computerChoice);

#2

Well choice1 and choice2 only exist in your function and the connection is done by compare(userChoice,computerChoice) which assigns the first argument userChoice to the first parameter choice1 and so on.


#3

It goes through the first game with results and all but that's when I get the "choice is undefined" message. How do I fix it to continue the game?


#4

Have you updated the code? Could you post the updated code? Do you understand how functions work? How you call them? How parameters are used? If not feel free to ask or review the lessons on this topic. And please be a bit more descriptive that makes it easier to see where things go wrong? :smile:


#5

I got it to work, thanks a lot!