I'm about to throw my computer


#1


I'm stuck on if choice 1 is paper


I keep getting unexpected keyword else


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


#2

take a look at this code with comments:

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";
        }
   // missing } to close your else if
    else if (choice1 === "paper") {
         if (choice2 === "rock") {
            return "paper wins";
         }
        else {
            return "scissors win";
        }
    // missing } to close else if
// missing } to close your function

this code has a very clear structure. You have an else if, which checks choice1, then inside this else if clause, you nest an if and else, but it is very important to close else if before moving to the next else if


Can't understand what's the problem. What if choice1 is rock?
#3

Perfect! that was exactly what I was missing. I thought I had closed everything out last night but I guess I didn't.

Are there any common tips to make sure everything is closed? I'm guessing just count your starting { and then count your closing } and see if they match up?


#4

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