Build Rock, Paper, Scissors... 7. What if Choice 1 = paper


#1

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

keeps saying unexpected token else, i have reviewed it several times and cant find the issue


#2

@jkmnmk,
You are forgetting to use an extra closing-curly-bracket-}
with which you close the ELSE-IF
before you start a new one....

The usage of a pair-of-curly-brackets-{ }
is to encapsulate a so-called code-block ( that is code which should stand together )

Please read
http://www.codecademy.com/forum_questions/559dcb4be39efe550f00006b
and concentrate on:

  • the creation of the compare function
  • the truth-table
  • the build of the IF ELSE-IF statement structure as part of the compare FUNCTION-BODY
  • the execution of the compare function (keeping in mind that the return statement is used )

#3

I'm getting the same error. i thew my code into a syntax checker and it just had me randomly remove, what I thought were, necessary curly brackets. Any way you could take a look at my before and after and tell me why these curly brackets weren't needed?

Before (this throws errors about having unneeded curly brackets)

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

After (this one satisfies the checker but it seems like there are some bad missing brackets in here)

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

#4

Hey, your "if" line has an extra curly bracket . If you get rid of that one it should pass. Im still confused on this one though.:frowning:


#5

hey i am also here for the same "else token error" but i also noticed on your code another bracket opening missing.
if (choice 2 === "scissors") here you need a curly bracket opening; >> same with if (choice2 === "rock") { return "paper wins"; ////

Here is mine so far and i still get a token else error.

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

Would appreciate any reply! :slight_smile:

Updated : nevermind, i found my error about the else token, i just needed to remove the else from `else if (choice1 === "paper" and add a 4th curly bracket in the end.) Hope this helps.. cheers!
new 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";
}
else if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
} else {
return "scissors wins";
}
}
};


#6

so just a whole post about my final code for all who might need a closer look.
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
compare(userChoice, computerChoice);
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
compare(userChoice, computerChoice);
} else {
computerChoice = "scissors";
compare(userChoice, computerChoice);
} 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);

Basically what my errors through the entire exercise were the closing brackets at the end of each if/else statement.. So a good tip from me would be that at the end of each final } else { you should close the brackets twice in order for the console to recognise your next statement which has to start with else if . Without the brackets you can try removing the else at the beginning of those else if statements like in the post above..
Once again , Code away!


#7

The hardest part of javascript so far for me is all these ■■■■ curly brackets. I don't seem to be the only one. It just seems so unorganized and chaotic. Almost every exercise so far, that was where I fell short, and at the end, I don't feel like I LEARNED anything, only tweaked code until it passed. Which, I'm sure is my fault, but that doesn't change how frustrating it is.


#8

To everyone struggling wtih syntax errors I hope it will inspire you to write your code carefully. This won't end as writing maintainable and well formatted code is as much a part of coding as doing great stuff with it. I have often been humbled when I am sent to a few hours of code scanning & troubleshooting merely from a syntax error.

There are many way you can compromise on sticking to a single formatting style & optimize your view of your code in the process of writing it. For example when I find myself with a short function, it's often easier to simply Include the statement on a single line than to wrap around another few lines.

You're compare function formatted above will now become 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 === "rock") { return "paper wins";  }
    else { return "scissors wins"; }
  }

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

Do pay attention to the consistency, I kept spaces between parenthesis & curly brackets function(choice1, chioce2) {; all spacing is consistent & the same. For the values on a single line { return "paper wins"; }, I kept a space from the statement & the curly brackets. And most importantly, whether you're using this format or the post above, keep your nested statements on their proper siblings indentation, they should line up, as well with their closing brackets.

How else can you avoid this problem? well in short validators. When you have a lot of code, and it's difficult to look at , a validator isn't biased and will tell you exactly what's going on. I would recommend you check out my topic for this very frequent issue here on the forums:

What's another way you can avoid this mistake before it occurs? Well take a look at the problem solving procedure I conjured up to help this individual over here: https://discuss.codecademy.com/t/what-if-choice-1-paper/61181/3

Despite it being down right frustrating, I hope it drives you all to develope troubleshooting skills along side your understanding of the language.


What is wrong with my code?
To learn it, you gotta 'do' it is blank code acceptable?
#9

Thanks emgo! Yours looked WAY more organized and pleasant to look at. I have since passed that part and re-doing my code in a neat manner was a huge help to that. I'll definitely be more mindful of keeping code organized going forward.