7. What if choice 1 is paper?


This code is saying Syntax error... Can someone tell me why?? thank you in advance.

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")


Hi @xaviermorgan24,

You've actually done such a good job writing this out here that rather than spoil this by giving you the answer I would like to give you something more valuable. :slight_smile:
Now of course, if we have a fancy IDE to hand we could spot errors as they were highlighted for us, but sometimes we don't have those, but what we do always have is good practice.

What I would like to do is set you a small task please, and hopefully this will help you find your syntax error in this code snippet and help you find many more in future ones.

Please go ahead and break your code down into any style format you would like and then reiterate that style through your function.
Let me explain if I may, for myself I always put curly braces on their own line and indent the code block inside. So if I was to take your example it would start something like this...

var compare = function(choice1, choice2)
    if(choice1 === choice2)   //indented this if inside the function compare  
    { //put this curly on it's own line, this is my opening curly
        return "The result is a tie!"; //indent the code block for this if
    }  //put the closing curly on its own line and put it in line with it's opening curly
    //... more code here
} //closing curly for my function, inline with it's counter opening curly

from here I would repeat this for all other code statement to make it look uniformed so if I had more if's or else they would follow the same style, curly on it's own line, with both the opening and closing curly lining up, and the code block to execute indented...

if ("I like code easy to read" == true)
   console.log( "I indent my code");
   console.log("why wouldn't I?");

The idea here is that other than the odd semi-colon etc. one can review over their code easily and see if things line up, or don't and then hopefully spot errors quicker.
Go ahead and try this, you can paste the result here if you like or not, but please do let us know if this helps and you pound those pesky syntax gremlins to the ground, or if you still need help, and someone will try to reply as soon as they can.

PS: I know it can get frustrating to spend ages on syntax problems, to have someone find it or discover it yourself and go "oh that was it" :slight_smile: so hopefully this technique will be of great benefit and you will be able to take advantage of it as you progress :grinning:



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