7. What if choice1 is paper?


#1

Below is my code. I have looked at other people's code and mine seems to look like theirs, however, I can not see what I'm missing. Can someone please tell me what they see or don't see in my code? I would truly appreciate the input. Thank you.

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

In what way does it behave differently from what you want? That probably says a lot about what is wrong.


#3

Your missing your ending brace for your else if statement! @mrs2nsmith fix that and you should be good.


#4

I'm getting a SyntaxError: Unexpected token else.


#5

And when would you expect an else? Whichever that situation is where you would expect an else to appear in your code, one of your else's isn't in that situation, that's what you're looking for.


#6

look at what i posted @mrs2nsmith that will help you solve your problem....

Your missing your ending brace for your else if statement! @mrs2nsmith fix that and you should be good.


#7

I'm not seeing where this brace is missing. It looks like I have too many already.


#8

Basically in your else if statement where it checks if the choice 1 is rock... You have two statements:

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

The if statement is opened and close.. good, the else statement is opend and closed , thats good to. BUT the else if statement is opened but not closed


#9

I'm sorry but it looks right to me. I don't see what's wrong with it.


#10

Look at your else's.
That's what the syntax error is saying, that's what you should follow to find the mistake.

else comes after if

Check each else.


#11

This is what I came up with:
var compare = function(choice1, choice2) {
if(choice1 === choice2) {
return "The result is a tie!";
}
if (choice1 === "rock") {
else if (choice2 === "scissors") {
return "rock wins";
}
else {
return "paper wins";
}
if (choice1 === "paper") {
else if (choice2 === "rock") {
return "paper wins";
}
else {
return "scissors wins";
}
}
};


#12

var compare = function(choice1, choice2) {
if(choice1 === choice2) {
return "The result is a tie!"
}
else if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins"
}
else {
return "scissors wins"
}
}
};

this is the answer :wink:


#13

Did I write it wrong totally?


#14

Don't think of it so much as in right or wrong.

You're writing instructions to be executed. If it doesn't execute the way you want, then observe where it differs from your expectations and adjust accordingly.

Errors happen when what you wrote can't be executed, they come with a message about what you tried to do. (In this case, an else where it can't be)

Have you verified that there is an if preceding each of your else's?

If you try to run just this code:

else {
}

Then you'll get the same error message. What is missing in the above piece of code?


#15

There is an if missing in front of choice1 === paper.


#16

Yes. So now you'll need to consider what you meant to happen there. What is it supposed to look like?


#17

This is so confusing.


#18

Does your indentation match your braces? Because you usually shouldn't need to count braces to figure out what belongs to what, indentation does a better job at this.

The last piece of code that you posted indents as follows:

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

See how it's running off to the right?


#19

The return is missing in that code.


#20

I see that. I guess I'm not supposed to take those out. I made that mistake on my own.