Rock, Paper, Scissors; I have a very odd situation!


#1

Hello World!

This is very odd. I'm on the last step of Rock, Paper, Scissors and my code works. In fact, when I submit, I'm prompted to move onto the next lesson. Here's the problem, though; when I run the below code and input "paper" for the userChoice prompt, it still works, but it prints "undefined" instead of "paper wins" (or some or answer wins). For rock and scissors, it prints correctly.

Why is it printing undefined when I input "paper"?


var userChoice = prompt("Do you choose rock, paper or scissors?");

var computerChoice = Math.random();

if(computerChoice < .34) {
    computerChoice === "paper";
} else if(computerChoice < .67) {
    computerChoice === "rock";
} else {
    computerChoice === "scissors";
}

var compare = function(userChoice, computerChoice) {
    if(userChoice === computerChoice) {
        return "The result is a tie!";
    } else if(userChoice === "rock") {
        if(computerChoice === "scissors") {
            return "rock wins!";
        } else {
            return "paper wins!";
        }
    } else if(userChoice === "scissors") {
        if(computerChoice === "paper") {
            return "scissors win";
        } else {
                return "rock wins";
            }
        } else if(userChoice === "paper") {
        if(computerChoice === "rock") {
            return "paper wins";
        }
    } else {
            return "scissors wins";
        }
    };


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


#2

In this section of your code, you have a 2nd closing bracket after the return statement of your nested if (computerChoice === "rock") condition. This means you are ending your nested if before it gets to your else { return "scissors wins" ; } code. So when userChoice is paper and the computerChoice isn't rock, it is undefined.


#3

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