SyntaxError: missing before statement (FINAL LESSON)


#1

I'm getting "SyntaxError: missing before statement".

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

}
}
};
compare(userChoice, computerChoice);

Can anyone please help me debug this code. The issue is somewhere within the last section. I'm finding it complicated to keep up with all the "{" brackets while coding in JavaScript, so I don't doubt that is the issue. It's a bit frustrating honestly...


#2

@colab.help,
The curly-brackets-{ }
are used the encapsulate statements which should stand together
forming so-called code-blocks

Using the proper indentation you could keep track of their usage.....

var compare = function(choice1, choice2){
    if(choice1 === choice2) {
        return "The result is a tie";
    }   <<<<<<<<< End of IF code-block

    else if (choice1 === "rock") {   <<<<< Begin ELSE-IF code-block
            if(choice2 === "scissors") {
                return "rock wins";
            } 
            else {
                return "paper wins";
            }
    }                                 <<<<<< End of ELSE-IF code-block

    else if (choice1 === "paper") {
             if (choice2 === "rock") {
                 return "paper wins";
             }   <<<<<<< End of IF code-block
             }      <<<<<<<<<<<<<<<<<<<<<<<<< Error !!!!!!!
             else {
                 return "scissors wins";
             }  <<<<<<<< End of ELSE code-block
    }

    else if (choice1 === "scissors") {
             if (choice2 === "rock") {
                return "rock wins";
             }     
             }     <<<<<<<<<<<<<<<<<<<<<<<<<<<< Error !!!!!!!
             else {
                 return "scissors wins";
             }
     }
};   <<<<<<<< End of =compare= FUNCTION-BODY

For the execution of this compare =function=
please read the section on executing-the-compare-function in
http://www.codecademy.com/forum_questions/559dcb4be39efe550f00006b


#3

Thanks! Your assistance is really appreciated...


#4

I guess my confusion is coming from how to determine how many closing brackets to use. In this example the first code block has 2 open brackets, but only one closing.

However, in the second section starting with the "else if" statement there are 3 total open brackets, but only 2 closing it.