What if choice1 is rock? help


#1

So far I've had trouble doing this and I don't know what I'm doing wrong...

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

#2

In line 4, you have a closing curly brace. Do you understand what it does?


#3

Not exactly, could you please explain?


#4

I believe you're closing the function too early - the if statement for choice 1 === choice 2 is closed fine, but then before the rest, the entire function is closed with that additional } on the fifth line. If you remove that, it should work unless I'm missing something else! I think it's just a simple syntax/typing error.


#5

I removed that, still not working. It comes up with the error "SyntaxError: Unexpected end of input"


#6

Add an extra one at the end instead


#7

Still not working - still has the same error


#8

Sorry. I'm new to this too, I just did that lesson a while ago. Whatever I did worked, but I can't see anything else wrong with that part of your answer - maybe it has something to do with the indentation, but someone else can prob give a more knowledgeable answer. I believe that syntax error has to do with the incorrect closing of brackets somewhere along the line though.


#9

It's now

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

#10

Can you possibly go back to that unit and post what you did?


#11

I got it... you need an extra } in this part:

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

I can't bring back my original answer but it works for me like this:

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

Edit: That if statement is nested inside the else if above it so the extra { closes the else if , but the extra one on the bottom from before is needed too to close the function as a whole


#12

Thanks for your help @jorpar!


#13

I just needed to add that brace there


#14

Yeah, I was just as confused as you, lol but if you indent properly it's easier to tell, mine is messy too


#15

Yeah thanks a heap, I was really confused where I was going wrong.


#16

No prob! Good luck with all of it going forward


#17

Ok thanks I'll do my best


#18

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