Lesson 6 syntax error (Rock, Paper, Scissors)


#1


For lesson six my code brings up syntax error although it should be right. I followed the code in the hint section and it says syntax error

Link" https://www.codecademy.com/en/courses/javascript-beginner-en-Bthev-mskY8/1/2?curriculum_id=506324b3a7dffd00020bf661#


The error message that shows up is: Syntax Error: "Unexpected end of input"

This was my 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";
    }
};

and this is the code I got to work:
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";
    }
};

in the second code there are two curly brackets after "rock wins" when there should only be one and this is according to the hint. Please tell me how the second code works while the first one does not.


#2

Look more closely at each pair of brackets. I've marked the pairs.
Your code:

var compare = function(choice1, choice2) { A
if(choice1 === choice2) { B
return "The result is a tie!";
} B
else if(choice1 === "rock") { C?
if (choice2 === "scissors") { D
return "rock wins";
} D
( } ) The missing end bracket C
else { E
return "paper wins";
} E
}; A


#3

But wouldn't the supposedly missing C bracket close off the if/else part
that is part of the "else if", so that there would only be the "if" part
but the "else" would be treated as the third part to the larger " if, else
if, else" code


#4

Ohhhhh. Yes, the second code doesn't work properly. Your original code is right. The reason your code doesn't work is because the missing bracket C isn't under "rock wins," but between end bracket E and end bracket A, so it includes the else in the else if code. Also, I looked back at the hint and it seems there is actually only one bracket after "rock wins." :sweat_smile:


#5

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