7/9 Syntax Error


#1

I keep getting the SyntaxError: Syntax error message.
Here's my code:
else if (choice1 === "paper"){
if (choice2 === "rock"){
return "paper wins";
}
else {
return "scissors win";
}
}

I don't understand what I'm doing wrong


#2

@faith_1920,
As you did not provide the full code
i can only assume that you are forgetting a closing-curly-bracket-} at the end,
with which you would close the compare FUNCTION-BODY.

Have a read
= http://www.codecademy.com/forum_questions/559dcb4be39efe550f00006b
and concentrate on

  • the build of the IF ELSE-IF statement structure

#3

I put three }'s at the end of the code and I'm still getting the same error message
Here's the full code:
/*var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} 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 win";
}
}
}


#4

@faith_1920,
you still have to concentrate on

  • the build of the IF ELSE-IF statement structure

You are closing the compare function-body to early
as you are using 3 closing-curly-brackets-}
to close the first-ELSE-IF statement part,
causing you to close the compare FUNCTION-BODY....


#5

OK, so how do I fix it? I have absolutely no clue


#6

upload your code so we can see please im stuck aswell


#7

/*var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} 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 win";
}
}


#8

I'm stuck on the same thing you are on right now, faith, my code looks identical to that. It has to do with the curly brackets...


#9

Well both of you made the same mistake

You use

else if (choice1 === "rock"){
    if (choice2 === "scissors"){
          return "rock wins";
    }
    else {
          return "paper wins";
    }
}
} //<== this one is TOO much...it closes the FUNCTION-BODY !!!

#10

Thank you we appreciate the help!!


#11

Your appreciation of the help is all well and good, but the code (with Leon's above adjustment) is still not accepted.
Oops, try again. There was a problem with your syntax.
SyntaxError: missing } after function body


#12

@kingmaxfield,
did you read the post http://discuss.codecademy.com/t/7-9-syntax-error/4838/2?u=leonhard_wettengmx_n
where a i suggest to use the ....6b link
and concentrate on the build of the IF ELSE-IF statement structure...

The error-message couldn't be more clear
SyntaxError: missing } after function body
in the near future they are going to produce computers which are able to bite you....

var compare = function(choice1,choice2) {
     //Begin of FUNCTION-BODY
     //
     // Here you place the IF
     // and one or more ELSE-IF
     //
     //End of FUNCTION-BODY
};

So if you look at your code...
at the End you would have 3 closing-curly-brackets-}
-1 one to close the ELSE of the IF ELSE statement
-2 one to close the ELSE-IF statement structure
-3 one to close the =compare= FUNCTION-BODY


#13

Thanks so much! You cleared up most of my problems! :grinning:


#14

Were you able to figure it out, I still can't


#15

If you made changes to your code...
present this =new= code....
we can then have a look at it...?!


#16

Yes i got it did you yet? It is this:
/*var userChoice = prompt("Do you choose rock, paper or scissors?");

var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} 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 if (choice1 === "scissors") {
if (choice2 === "paper") {
return "scissors wins";
}
}
}


#17

@roberto31691,
The ELSE-IF's for
choice1 equals paper
and
choice1 equals scissors
both have an incomplete IF ELSE statement structure !!