7/9 build rock paper scissors unexspected else


#1

What is wrong with my code need help
var compare = function(choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie!";
}
};
var compare = function(choice1, choice2){
if(choice1 === choice2) {
return "The result is a tie!";
}
else if(choice1 === "rock" && choice2 === "scissors") {
return "rock wins";
}
else {
return "paper wins";
}
} else if (choice1 === "paper"){
if (choice2 === "rock"){
return "paper wins"
}
else{
return "scissors wins"
}


#2

You only need:

var compare = function(choice1, choice2){
 if(choice1 === choice2) {
 return "The result is a tie!";

to appear once. so get rid of the first one and see if that fixes your issue.


#3

Now its say illeagle return statment


#4

could you post your new code?


#5

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


#6

your problem is right here:

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

ok so try this instead:

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

The reason for this is because the else is going to the "else if" statement and ending the "else if" train. basically you need to nest your if statements so that you can have an else if statement afterwards. Otherwise you'll have to be repetitive. The way you're doing it it would look like this.

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

#7

Hi there, what is wrong with my code? I keep getting the **

(SyntaxError: expected expression, got keyword 'else') ** error message

var compare = function(choice1, choice2) {
if(choice1 === choice2) {
return "The result is a tie" ; // semicolon
}

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