Keep getting "undefined" after completing all the code


#1
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 (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 === "paper") {
        if(computerChoice === "rock") {
            return "paper wins";
        }
        else {
            return "scissors wins";
        }
    }
    else if(userChoice === "scissors") {
        if (ComputerChoice === "rock") {
            return "rock wins";
        }
        else {
            return "scissors wins";
        }
    }
}

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

I dont understand why my code returns an "undefined" instead of telling me who wins...Anybody able to help?


#3

For starters, your function should have locally defined parameters, choice1 and choice2, which map to the arguments in the function call, userChoice and computerChoice.

In your above code, the last case has ComputerChoice which is not defined. If none of the cases are matched (flow does not branch out) there is no return value from the function so JavaScript responds with undefined.

The SCT is quite explicitly expecting exact text match for this line:

return "The result is a tie!";

#4

I am sorry if this sounds like a stupid question - but what does it mean that they "map out" to the arguments? I think I missed something.


#5
compare = function (choice1, choice2) { // ... };
                       ^        ^
                       |        |
              /--------/     /--/
              |              |
              ^              ^   
compare(userChoice, computerChoice);