9. Next Steps - Rock Paper Scissors


#1

Hello everyone, I am on topic 9. Next Step.
I have added a loop which prompts the user to add the correct choice.
The issue is that I think i might have messed up my code because i get the following error: ReferenceError: rock is not defined.

To reproduce the error:
1. When prompted write scissors.
2. Observe error. (if the error does not immediately appear repeat step 1)

What did I miss? Thank you!

  var userChoice = prompt("Do you choose rock, paper or scissors?");
    console.log("User choice" + " "+ userChoice);

    while (userChoice !== 'rock' && userChoice !== 'paper' && userChoice !== 'scissors') 
    {
        userChoice = prompt('Select again.');
    } 

    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 compare (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(choice2 === "paper")
            {
                return "scissors wins"
                
            }
        
    };
    compare (userChoice,computerChoice);

#2

In your second and third else if you use this:

if(choice2===rock)

where rock is without the necessary "" so it is considered a variable and not a word therefore you get the error that this variable is not known to the interpreter.


#3

Thank you haxor789 !
Much appreciated!