ReferenceError: rock is not defined


#1



https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/1/4?curriculum_id=506324b3a7dffd00020bf661


I'm tearing my hair out over this one, I can't see any difference between rock, paper and scissors in the code but every time rock is either a computer or user choice I get the reference error 'rock is not defined' returned. Code below


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("User: " + userChoice);
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 "paper wins";
        }
    }
}

compare(userChoice,computerChoice)


#2

rock ,paper, scissors these are not variable.these are string.Strings are written inside double or single quotes. Numbers are written without quotes.
so you have to do like this

else if (choice1 === "rock") {
if (choice2 === "scissors")
....................................


#3

Many thanks, have updated as advised but now I get this message:

"Oops, try again. Your code returned 'paper wins' instead of 'undefined' when the inputs are scissors and paper"

(in this instance userChoice was "rock" and computerChoice "paper", so no one actually chose scissors)


#4

can you please give me your update code?


#5

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("User: " + userChoice);
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 "paper wins";
    }
}

};

compare(userChoice,computerChoice)


#6

you have taken two times

choice2===rock

so change it like

else if (choice1 === "scissors") {
if (choice2 === "paper") {
return "scissors wins";
}
else {
return "rock wins";
}
}


#7

thank you for your help, I have updated the code (see below), unfortunately I now get the message "Your code returned 'paper wins' instead of 'undefined' when the inputs are scissors and paper". Argh!

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("User: " + userChoice);
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 "paper wins";
    }
    else {
        return "rock wins";
    }
}

};

compare(userChoice,computerChoice)


#8

ahh i see the problem here in the last statement - have fixed and now works!