6. What if choice1 is rock?


#1

So its giving me "SyntaxError: missing before statement" and I can't seem to understand what that means. Here's 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";
    }
}

#2

Check all your braces. One opening brace is missing (on if line) and one closing brace is missing (at the end of the function body).


#3

Thanks. I got it alright after a few tries. The brackets always make a problem for me.


#4

To help work out the brackets in a complex structure, break it down and block it out in advance, then fill in the statements afterward.

var identifier = function (params) {
    // code
};

identifier(arguments);

That is the basic function with parameter list and code body. The call statement is written at the same time so we can match the name of the function, and write the arguments.

Here is the basic if..else if..else

if (condition) {

}
else if (other_condtion) {

} else {

}

Now all the braces are in the right places, and the statements can be written in.


#5

Yeah, but when doing a large one like Build "Rock, Paper, Scissors". You have to be cautious and check every line to make sure it isn't wrong which gives me real headache.


#6

This project uses a repeated pattern for all the else if's which we can block the following way:

if (condition) {

}
else if (state1) {
    if (state2) {

    } else {

    }
}

This pattern repeats three times, but only do one for each lesson. do not race ahead or your code will not be accepted.


#7

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