What is wrong with this: SyntaxError: expected expression, got keyword 'else'


#1



https://codecademy.com/en/courses/javascript-beginner-en-Bthev-mskY8/1/3?curriculum_id=506324b3a7dffd00020bf661


SyntaxError: expected expression, got keyword 'else'
why is this not working... it worked for the previous lesson (without the second scenario)
anyways can someone help?

all of code here, is there something wrong?

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

Your code should have this basic outline:

if (condition) {
// do something
}
else if (condition) {
if (condition) {
// do something 
} 
else {
// do something else
}
}

#3

It's much easier to both prevent errors and troubleshoot if you use indentations when coding. There's a nice little page that automatically "beautifies" javascript at http://jsbeautifier.org/. Your code beautified turns out like:

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

Now it's easy to see where the code is wrong.


#4

still a little bit confused... i think i deleted the second bracket after

but it still gives me SyntaxError: expected expression, got keyword 'else'
what else am i missing?


#5

Basically you have a bunch of brackets in the wrong places. I'd start over and copypaste/rewrite it more carefully.

edit: It should look something like this:

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 wins";
        }
        else {
            return "paper wins";
        }
    }
}