What if choice1 is paper?


#1

Totally lost here, getting "Syntax Error: Unexpected token else". Any help would be greatly appreciated

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


#2

There is no opening bracket here after the condition,

if (condition) {


#3

Fixed but still getting the same error

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


#4

Couldn't figure out what was wrong but i knew the first part

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

worked so I just copy and pasted from else if to } which I knew worked and replaced the information with the corresponding "paper", "rock", and "scissors" and it worked perfectly. still can't figure out where it went wrong the first time though


#5

You are missing one closing bracket to make your code work. Pay attention to this elif,

}


#6

Ahhh thank you very much, I will make sure I do in the future


#7

Hi, I'm getting the error: "SyntaxError: Unexpected token else"

Here's my code:
/*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";
 }

};

Where is the problem? Thanks in advance


#8

you're missing some brackets in if / else / else if statements. Compare your code to mine and you'll see what are you missing, cheers.

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 if (choice2 === "paper"){
                return "scissors wins"; 
                }
         }      
        
};

compare (userChoice, computerChoice);


#9

Those brackets always get me. Thanks!


#10

Hi, I checked this exercise, but something does not work. Could you please help me?
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";
}
}
};


#11

The syntax for your else if statement for choice1==="paper" is correct. The else if statement for choice1==="rock" is off. Look at the hint in the lesson as well and make the corrections to your code.


#12

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.