8 Your code returned 'paper wins' instead of 'undefined' when the inputs are


#1

I've looked everywhere but can't seem to find the problem. Pls 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( choice, 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(choice2 === "rock") {
return "rock wins";
}
else {
return "paper wins";
}
}
};
compare( userChoice, computerChoice)


#2

Hi this part

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

Should have this strutture

else if (condition) { 
   if (condition ) {

   }

   else { 

   }

}

In the condition of the else if statement you should put the choice1 and in the if statement choice2


#3

so i finish with an else if statemente instead of an Esle statement?
I did that and it still didn't work:
else if(choice1 === "scissors") {
if(choice2 === "rock") {
return "rock wins";
}
else {
return "paper wins";
}
}
};
compare( userChoice, computerChoice)


#4

Hi what error did you get and can post your new code.


#5

Sure thing:
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( choice, 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)

ERROR: Your code returned 'paper wins' instead of 'undefined' when the inputs are scissors and rock

(Thanks for taking the time)


#6

Hi here

else {
return "paper wins";
}

try to change "paper wins" by "scissors wins"


#7

Don't think that's the issue. Still did it but changed nothing


#8

Here

var compare = function( choice, choice2 ) {

You wrote choice instead of choice1


#9

That was a copying mistake, i wrote it in the code. Like so: 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";
}
}
else {
if(choice2 === "rock") {
return "rock wins";
}
else {
return "paper wins";
}
}
};
compare( userChoice, computerChoice)


#10

Your code should be like that

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