This forum is now read-only. Please use our new forums at discuss.codecademy.com.

581 points
55ecc5cc93767619b6000357_295993857
Submitted by
Ishraq
almost 4 years ago

Please help me fix this

There's clearly a syntax problem, that much I know.

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";{


1 vote

permalink

Lots of issues with brackets (braces, actually):

compare = function(choice1, choice2){
    if (choice1 === choice2){
        return "The result is a tie!";
    }         // good up to here
};            // closing the function body before end of input

    else if (choice1 === "rock") } // wrong brace; should be {
        if (choice2 ==== scissors"){
            return "rock wins";
        }
        else    // missing {
            return "paper wins"; 
        { // wrong brace; should be }
    // missing a } brace
}; // closing brace for function body should be here

8799 points
5958b8ebe12813a2a3000034_980106998
Submitted by
Roy
almost 4 years ago

2 Comments

55ecc5cc93767619b6000357_295993857 Ishraq almost 4 years ago

Wow, yep I have a lot of trouble with using braces. But the comments really helped me understand where I went wrong, thank you so much for putting in the extra effort!

5958b8ebe12813a2a3000034_980106998 Roy almost 4 years ago

I like to look at things from the outside in, then block out my structures at the beginning. See below:


0 votes

permalink

The outside blocking:

var compare = function(){
    // everything in here
};

Inside blocking:

    if (){
        // code
    } else
    if (){
        if (){
            //
        } else {
            //
        }
    }

For good measure, the function call expression AFTER the compare() function body (this is not expected but will still pass)

compare(userChoice, computerChoice); // our globals

We see that our function needs two parameters to correspond with these arguments.

var compare = function(choice1, choice2){
    // code
};

Now we can fill in the missing bits to the inside blocking:

    if (choice1===choice2){
        // code
    } else
    if (choice1==="rock"){
        if (choice2==="scissors"){
            //
        } else {
            //
        }
    }

And finally put it all together:

var compare = function(choice1, choice2){
    if (choice1===choice2){
        // code
    } else
    if (choice1==="rock"){
        if (choice2==="scissors"){
            //
        } else {
            //
        }
    }
};

Now all it needs is the return statements, as apply. We can see that all our syntax is correct up to this point.

return "The result is a tie!";
return "rock wins";
return "paper wins";

8799 points
5958b8ebe12813a2a3000034_980106998
Submitted by
Roy
almost 4 years ago