Oops, try again. Your code returned 'paper wins' instead of 'scissors wins' when the inputs are paper and scissors


#1

Oops, try again. Your code returned 'paper wins' instead of 'scissors wins' when the inputs are paper and scissors

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 === choice2 ){
 return "The result is a tie!";   
 }

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

}


#2

According to this part of the code:

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

If choice1 is not a "rock" the result is "paper wins".


#3

i dont understand :confused:


#4

This is part of your code. It basically says:

If choice1 is "rock" and choice2 is "scissors" return "rock wins", if choice1 is not a "rock" return "paper wins".

But what we want to achieve is this:

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

Do you see the difference?


#5

is it the { } ?? what do these actually mean ?


#6

Yes, we use braces to enclose blocks of code, for example when we create if we have to specify a block of code that will be executed when the condition of if evaluates to true. We can omit braces if the code that we want to execute has only one line.

This is correct code for this exercise:

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

Please compare it with your own code and pay attention to braces. If there is something that you don't understand please do not hesitate to ask.


#7

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