8/9 Rock, paper, scissors - "ReferenceError: Paper is not defined"


#1

Please help me! I've coded the rock, paper, scissors game, and when I tested it the first couple of times it seemed to work fine. But now it tells me "ReferenceError: Rock is not defined" (it's different whether it's rock, paper og scissors that's not defined for each time I try).

My code:

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

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!";
        }
    }    
};

console.log(compare(userChoice, computerChoice));

#2

Hi, you should put string in if condition for this sample code.

if(choice2 === "scissors") {

if(choice2 === "rock") {

if(choice2 === "paper") {

#3

Thank you! I realised this 1 second ago. It all makes sense now :yum:


#4

You are forgetting the " from many string with paper, scissor rock etc (look at your if statements)!!!!!

Just saw pointphoton had replied :wink:


#5

@javajumper12468,
and the return-Values should be in lowercase
like
"rock wins"
"paper wins**
"scissors wins"