Oops, try again. Your code returned 'paper wins' instead of 'undefined' when the inputs are scissors and paper


#1

Please help me. Thanks


#2

Please post your code so we can help you find out what's wrong.


#3

I have this problem too. Code works fine, but I get this error: Here's my code & error.
Oops, try again. Your code returned 'paper wins' instead of 'undefined' when the inputs are scissors and rock

var compare = function (choice1, choice2) {
console.log("inside compare", choice1, choice2, result);
if (choice1 === choice2) {
result = "The result is a tie!";
} else if (choice1 === "rock") {
if (choice2 === "scissors") {
result = "rock wins";
} else {
result = "paper wins";
};
} else if (choice1 === "paper") {
if (choice2 === "rock") {
result = "paper wins";
} else {
result = "scissors wins";
};
} else if (choice1 === " scissors") {
if (choice2 === "rock") {
result = "rock wins";
} else {
result = "scissors wins";
};
};
return result;
};

var userChoice = " ";
var computerChoice = " ";
var result = " ";
userChoice = prompt("Do you choose rock, paper or scissors?");
computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
};
console.log("before compare", userChoice, computerChoice, result);
result = compare(userChoice, computerChoice);
console.log("after compare", userChoice, computerChoice, result);

My inputs were:
before compare rock paper
inside compare rock paper
after compare rock paper paper wins
inside compare scissors rock paper wins ---- don't know why this prints out, should have ended after calling compare. Not sure what is going on. Please help, thanks, Carol


#4

OH! I found a space in " scissors" that screwed up everything! My bad.


#5

Hi, am also facing the same issue. Kindly help me to rectify it.

Here is my code :


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 === "scissors")
{
return "Scissors Win!!";
}
else
{
return "Paper Wins!!";
}
}
else if (choice1 === "scissors")
{
if (choice2 === "rock")
{
return "Rock Win!!";
}
else
{
return "Paper Wins!!";
}
}
else
{
return "Undefined";
}
};
var userChoice = " ", computerChoice = " ";
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);
compare(userChoice,computerChoice);

Thanks in advance!! Happy Learning !!