I see most people get stuck in the same place with rock,paper and scissors


#1

i have rewritten this code several times , and i still cannot find out what is wrong with it.
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

Check the syntax for your if statement. You followed correct syntax for your else if and the nested if and else statement within it. Don't forget to wrap up your compare function correctly as well.

Hint: There are 2 missing braces in your code


#3

thank you, i get it right now. but in previous exercises i did not have to use 4 braces ..check link below? https://www.codecademy.com/en/courses/javascript-beginner-en-Bthev-mskY8/1/1?curriculum_id=506324b3a7dffd00020bf661


#4

I'm not clear on which 4 braces you didn't need to use. Maybe you could share your code from the prior exercise?


#5

var compare = function( choice1,choice2) {
if(choice1===choice2)// < the missing brace, but why the code is right? i am sorry for my english

return "The result is a tie!";
}

here my friend.


#6

I see what you did here. Technically, not using curly braces for if and else statements is fine. The syntax problems can arise when you have multiple if/else if and else statements, like in your code below.

The curly braces act like containers for code waiting to execute depending on whether or not conditions are or aren't met.

The curly braces act like containers for code waiting to execute when conditions are or aren't met. When your braces don't correspond to the code within them, you'll get syntax error messages.

You should try removing all braces from the if/else if and else statements and see what error messages you get. Or if you get them at all.

I would still use curly braces for your compare function.


#7

thank that was helpful.


#8

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