Your code returned 'paper wins' instead of 'undefined'


#1

Hi there, I hope you can help a bit with this, cause I cannot see it through.

The code seems to work very well, the game works and all is fine, except for the fact that I get this unexplicable error: "Oops, try again. Your code returned 'paper wins' instead of 'undefined' when the inputs are scissors and paper".

Here is my code:

var userChoice = prompt("Do you choose rock, paper or scissors?");
console.log("Me: " + userChoice);
var computerChoice = Math.random();
console.log(computerChoice);
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 wins";}
    }
    else if (choice1 === "scissors") {
        if (choice2 === "rock") {return "rock wins";}
        else {return "paper wins";}
    }
};
compare(userChoice, computerChoice);

#2

Not saying it's wrong, but you don't necessarily need if (choice2)


#3

Hi in 8. What if choice1 is scissors? your else statement the instruction says

b. if choice2 === "paper", then "scissors" wins.
so the last else statement

else {return "paper wins";}

schould return "scissors wins"


#4

The lesson is n. 8. "What if choice1 is scissors?"


#5

Marcos got it. Hopefully that fixes any issues.


#6

Wow! It worked!! Thanks a lot guys!


#7

yes but did you read my all my post ?


#8

Yes I did. Still trying to understand why it worked though


#9

Read b.

if choice2=== "paper" then "scissors" wins
It shouldn't be paper wins it needs to be scissors in order for it to work.


#10

Yeah, was just re-reading all and figured it out. My way could not possibly have worked....


#11

Always read the instructions carefully, it helps. Good luck!


#12

you could find out why it gave you an error on that discussion..
https://www.codecademy.com/forum_questions/559dcb4be39efe550f00006
on the third post