7.What if choice1 is paper?


#1

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

It says SyntaxError: Unexpected token else

What's the problem?

Thank you for your attention!


#2

this bit off code:

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

you need to close your if choice1 is scissors before your else if choice1 is paper.


#3

i dont get it. can you explain


#4

well, the if and else should be nested inside your else if, which means when you are going to your next else if(choice1 is something), you need the closing curly bracket for the previous else if. Which is missing

But if you have a question, please create your own topic with code + error message + question


#5

WHAT I DONT GET IT. it should be easy


#6

well, the questions you are creating don't exactly help, since you don't explain what it is you don't understand, it is simple, you have a else if to check what choice1 is:

else if (choice1 == "paper"){
  
}

inside your else if, you have a if and a else statement, to check what choice2 is:

else if (choice1 == "paper"){
  if (choice2 == "rock" ){
    // return paper wins
  } else { 
    // return scissors wins, since choice2 is scissors
  }
}

it is simple, you just need to keep good track of your brackets and take time to read the explanation and don't rush


#7

That explains but why choice two scissors


#8

well, it can't be paper, because then the result would be a tie. It can't be rock either, since we check for it. So the only option remaining is scissors.


#9

Thanks when i got the help it still did not work


#10

seems mtf is helping you out, you are in good hands.


#11

thanks but like why isn't it working


#13

Too soon to say. I haven't actually helped with anything yet.

Consider the following pattern:

if () {
    // tie
}
else if () {
    if () {

    }
    else {

    }
}
else if () {
    if () {

    }
    else {

    }
}

Pay close attention to the curly braces. They are critical to the program running correctly.