7. What if choice 1 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";
}

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

I just can't figure out what I'm doing wrong, it keeps telling me "Oops, try again.
Your code returned 'paper wins' instead of 'scissors wins' when the inputs are paper and scissors"


#2

This is your code after beautification and with additional comment:

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 { // this else is related to `if (choice1 === choice2) {`, not to `if (choice2 === "scissors")`
    return "paper wins";
  }
  if (choice1 === "paper") {
    return "paper wins!";
  } else if (choice1 === "paper") {
    if (choice2 === "rock") {
      return "paper wins";
    }
  } else {
    return "scissors wins";
  }
}

Take a look at indentation levels, they will help you keep track of flow in your script :slight_smile:

Also, it's a really good practice to always use curly braces around the block of if or for, even if there is only one line of code inside the block.

So instead of:

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

write:

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

#3

Thank you! I managed to change that but it keeps telling me the same thing :frowning:


#4

A post was split to a new topic: 7. What if choice 1 is paper


#5

Can you show me the code after correction? :slight_smile:


#6

Sure, sorry for late reply

and it tells me "SyntaxError: expected expression, got keyword 'else' "

I sense there is a problem in the first part but I just cannot figure out the logic of if and else if in that part


#7

I resolved it now!  


#8