userChoice, computerChoice


#1

Here is my code and I cant pass onto the next question because this keeps appearing
"Oops, try again. Your code returned 'scissors wins' instead of 'undefined' when the inputs are scissors and rock"

Can anyone help?

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!";
}

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";
}

};
compare (userChoice,computerChoice);

#2

Disable the execution of your compare function like
//compare (userChoice,computerChoice);
maybe that helps...?

Have a read
http://www.codecademy.com/forum_questions/559dcb4be39efe550f00006b
and have a look at executing the compare function for the near future...


#3
var userChoice = prompt("Do you choose rock, paper or scissors?");var computerChoice = Math.random();if (computerChoice &lt; 0.34) {	computerChoice = "rock";} else if(computerChoice &lt;= 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";}

};compare (userChoice,computerChoice);

This is what you did above.
This is what I did below.

   var userChoice = prompt("Do you choose rock, paper or scissors?");var computerChoice = Math.random();if (computerChoice &lt; 0.34) {	computerChoice = "rock";} else if(computerChoice &lt;= 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";}
    
    };compare (userChoice,computerChoice);

Let's do some compare and contrast, eh? :smiley:


#4

@ragezapper

as mentioned in prior =reply=


#5

Well, if we take out the compare function, it won't compare and show the results of who wins and who loses. That's pretty much why I am not taking out the compare function.
(which I think)


#6

@ragezapper,
I meant to point you at

https://www.codecademy.com/forum_questions/559dcb4be39efe550f00006b#answer-559ddf0a76b8fe8bf600039a


#7

Guess this one is the problem:

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

}else {
    return "scissors wins";
}

if you have a look at the other ones e.g.

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

}

you see that it always is an else if for choice1 and a nested if/else for choice2. Although it would be much easier to see if it would look formatted like this, would it?

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

Now the problem with your code above is that in this last case you close the else if after the if so the else is the default case for the else ifs but not for the

 if (choice2 === "rock"){

inside of:

else if(choice1 === "paper") {

as it should be.