Need help with Rock, Paper, Scissors


#1


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


I get the error message "Oops, try again. Your code returned 'Paper wins' instead of 'undefined' when the inputs are scissors and rock".

When I run the code it works with some combinations while it gives wrong result with some, though I get the error message always. For example, if the userChoice is rock and computerChoice is scissors it correctly states that Rock wins, but if the userChoice is paper and computerChoice is scissors it states that Rock wins.

My code is:

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 win";
        }
    } else if (choice1 === "scissors") {
        if (choice2 === "rock") {
            return "rock wins";
        } else {
            return "scissors win";
        }
    }
};

compare(userChoice, computerChoice);


#2

You're problem is that you have a typo one the line where you compare choice1 with "rock", you have a capitalized variable name. You should change Choice1 to choice1


#3

That fixed it, thanks for help. I really need to learn to be careful with capitalization, seems to be a problem quite often.


#4

Whenever you find an error like this in the future, try to check this first then. That way you'll never miss it. While evolving as a developer, try to make your individual checklist of frequent errors. Having these on your checklist will automatically make your checklist shorter, which makes sure that you're always improving.


#5

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