6. What if choice one is rock? I cant get my code to work, help?


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-Bthev-mskY8/1/2

I keep getting the syntax error: 'Unexpected token else' and i'm not really sure why, can anyone help?


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


#2

The semi-colon is terminating the if statement so that the else looks like the start of a new one. Remove that semi-colon and place it at the end of the line above it.

Also, remove the very last one, as well. If statements don't have ; on their blocks. The two lines inside the branches do get semi-colons, though.

            return "rock wins";

#3

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

#4

I keep getting the "unexpected token return" error, do you know why?


#5

Missing a ) on the conditional expression.

Notice that when I formatted your code, I added a semi-colon to the very end. Assignment statements always get a semi-colon:

var compare = function () {

};

#6

Now I am just getting the error "unexpected token 'else'"


#7

Try your hand at formatting your code after you paste it in, one more time.


#8

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

#9

Missing opening brace {.