Build "Rock, Paper, Scissors" - What if choice 1 is paper?


#1

Hi, I don't seem to get this right. I tried every possible combination but I could get the correct code in order to procceed.

I have been stuck on this choice1 === paper option for quite some time now

I keep on getting the "unexpected token else" error message every time I add or reduce closing braces. Obviously that's a syntax error

Here is wat my code looks like so far

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

Can anyone help please?


#2

Hi you should edite your post and post your code so that we could help you.


#3

Yes, just did that. Cheers


#4

This part you closed the else if statement earlier and remove the semicolon you after the closing } of the if statement in your code

else if (choice1 === "rock") {
    if (choice2 === "scissors") {
        return "rock wins";
    }; <-- #remove the ;
}  <-- #Remove this } because it close the else if statement
    else {
	return "paper wins";
    } 
} <-- #Add it here }

the same here

else if (choice1 === "paper") {
    if (choice2 === "rock") {
        return "paper wins";
    };
} 
    else {
        return "scissors wins";
    };
};

its also missing the closing } of the else if statement and remove the ; after teh closing } of the fi statement also to the else statement


#5

follwing your instructions here's how it's supposed to be isn't it?

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


#6

Now you should add one } at the end of your code for closing the compare function

var compare = function(choice1, choice2) {
   //Here put the if , else if and else statement
};

#7

Great!!! That worked thanks a lot!!

Please help me understant; The first part of the first code I pasted (where choice1 === rock and where choice1 and 2 are equal) I put the exact same thing as it was and it was valid. How is that possible with the ;} I put in my else/if statement ?


#8

you can Read more about the it.


#9

Ok cheers. I'll check that out. Thanks a lot.


#10

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