[8/9] 'Undefined'


#1

Hi, this is my code. For some reason I get the error "Oops, try again. Your code returned 'paper wins' instead of 'undefined' when the inputs are scissors and rock". What is the problem?

 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!";
    }
    
    if (choice1 === "rock") {
         choice1 === "rock"; }
    else if (choice2 === "scissors") {
         return "scissors wins"; }
    else {
         return "paper wins";
    }
    
    if (choice1 === "paper") {
        choice1 === "paper"; }
    else if (choice2 === "rock") {
        return "paper wins"; }
    else {
        return "scissors wins";
    }
    
    if (choice1 === "scissors") {
         choice1 === "scissors"; }
    else if (choice2 === "rock") {
             if (choice2 === "paper") {
                 return "rock wins"; }
         else {
     return "scissors wins";
          }
     
}
compare (userChoice,computerChoice);
};

#2

Ok it seems you didn't understood the concept but managed to find a work around. The problem is that for the last case you got confused with the two options :smile:

The general idea is that in the first approach you just have a look at the options for choice1 which are

tie 
rock
paper 
scissors

These are the 4 if cases that you have as well but as you already saw the choice1 === xyz case is rather useless:

 if (choice1 === "paper") {
    choice1 === "paper"; }

still you need it for the others. So the original idea was to nest them inside like this:

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

the [else] means that it is optional. For the last statement you seem to mix those two ideas and end up at:

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

And as you can see it doesn't make much sense as you always pick scissors wins or undefined. Last but not least move this:

compare (userChoice,computerChoice);

outside of your function as it is useless inside of it and might even be dangerous if you manage to actually reach it.