简体   繁体   中英

JavaScript Else If Statements

I'm doing Codecademy to get a better understanding of JavaScript and it's saying the code I'm writing is wrong. But I don't know where it's going wrong. The debug message coming up is:

"Missing an identifier and instead saw 'else', missing ';' before statement"

This issue comes up a lot as I'm writing can anyone let me know what exactly that debug message means so I don't have to come crawling back to you experts? Haha. Anyways, here's the code:

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

It should be

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

compare();

I can only guess what this function is supposed to look like

Easy to see if you properly indent your code:

var compare = function(choice1, choice2) {
    if (choice1 === choice2);
    return("The result is a tie!");
}; // this ends the function, not the if!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM