Syntax error in final step, coding rookie needs help


#1

Hey guys,

I'm totally new to writing code and just wanted to have some fun with this first course to see if I like it. And I do, but it's totally frustrating to keep staring at it and not finding the error. Looked through all the posts on here as well...

The error I get is: SyntaxError: Unexpected token else
Please help me understand what is going wrong here.
This is 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";
    }
    else if (choice1==="scissors") {
    if (choice2==="rock" {
        return "rock wins";
    }
}
    else {
        return "scissors wins";
    }
};
compare(userChoice, computerChoice);

#2

Hi all your else statement should be inside the else if statement like that in the first case.

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

#3

Seems like your problem is similar to this one:


#4

Thanks guys for the quick reply!
I've managed to get the else inside the else if statement, but now the error is "unexpected token {"

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

#5

You are missing a ) here.


#6

Oh haha, staring at it makes you blind :wink: Thanks for your help, it works now!