Rock, Paper, Scissors... getting undefined Math()


#1

Every time I run my program I get "Oops, try again. Did you set var computerChoice = Math.random() ? The capital 'M' in 'Math' is important!"

The program runs flawlessly yet it wont let me continue on. What am I doing wrong here?

::Heres my code::

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);
console.log("Human: " + userChoice);

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

It seems your missing an else statement within your function, in which if the user answers the question other than "Rock Paper or Scissors" it would return please try again or whatever you want to say. Here's where you should put it:

else if (choice1 === "scissors") {
        if (choice2 ==="paper") {
            return "scissors wins";
        }
        else {
            return "rock wins";
        }
    }
    
    else {
        return "You are by no means permitted to use such words in this game, please try again.";
    }
};

#3

That's a pretty good idea but not necessarily required for the syntax :slightly_smiling:

@arcpro03480 Which exercise are you working on? Because this error often occurs when you are ahead of the game and already work with computerChoice being a string when the exercise expects it to be a number.