7. What if choice1 is paper? NEED HELP!


#1


I've been stuck on this for the past day! Can anyone help me out please? I don't know the error I seem to be making - I'm guessing I've placed one too many brackets or just missed one out but I've tweaking it and it's still not working. Whenever I try to submit it says that there is an unexpected token. Can anyone help me please??


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


#2

else {
       return "paper wins"; { //WHAT'S THIS BRACKET FOR?
        }

You have an extra curly bracket


#3

I've got rid of that extra curly bracket but now when I go to submit, I get this message: SyntaxError: Unexpected end of input

I don't understand what else I've done wrong??


#4

You have a curly brace where there shouldn't be one, as already stated, and you don't have a curly brace where there should be one.

I've changed the formatting of your code just because i find it easier to finds things like this with it. I commented where it goes, but didn't actually add or remove curly braces, only commented where the errors are.

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"; { // <---remove this, extra curly bracket D:
}
}
else if(choice1 === "paper")
{
if(choice2 === "rock")
{
return "paper wins";
}
else
{
return "scissors wins";
}
// <--- Needs to be a } here to close up your last else if statement(choicce === "paper")
}

Edit: Okay so this isn't exactly how i format it but the forum sucks...anybody know which keys on the keyboard are the like ticks for code???


#5

To turn it into code... there is a button that looks like <./> I'lll do your code in that format for you. :grin:


#6

This is demtemblem's code but formatted :grin:

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"; { // <---remove this, extra curly bracket D:
}
}
else if(choice1 === "paper") 
{
if(choice2 === "rock") 
{
return "paper wins";
}
else 
{
return "scissors wins"; 
}
// <--- Needs to be a } here to close up your last else if statement(choicce === "paper")
}

#7

Thank you both so much! Can't believe I was missing one simple bracket in my code!

It works now! :grin:

Appreciate the help!


#8

Much appreciated...but don't give me all the credit. Give demtemblem the credit...it's his code :wink:


#9

A lot of the time...if not almost all of the time it's something so simple and so small that you overlooked it and then you have to pay for it by scrutinizing your code, matching parantheses, squiggly brackets, counting colons, etc.

90% of coding I swear.


#10

I'll get it right at some point! Thanks again!


#11

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