Lesson 7: What if choice1 is paper?


#1



SyntaxError: Unexpected token else


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 "rock wins"
}
}


#2

var compare = function(choice1, choice2) 
{
     if(choice1 === choice2) 
     {
        return "The result is a tie!";
     }
     else if (choice1 === "rock") 
     {
          if (choice2 === "scissors") // where is your open curly brace
               return "rock wins"
          }
          else 
          {
            return "paper wins"
          }
     else if (choice1 === "paper") 
     {
          if (choice2 === "rock") // where is your open curly brace
             return "paper wins"
          }
          else 
          { 
             return "rock wins"
          }
  // closing curly brace is missing 
}

#3

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 "rock wins"
}
}
Thats what my code looked like


#4

Please provide more structure in your code. Then you will also find your error faster
Look at my code above. Pay particular attention to my comments. And change it in your code. Closed and opening braces are missing. As I have written.


#5

sorry this is the updated code
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 "rock wins"
}
}


#6

Why are the else outside your else if?

if(choice1 === choice2) {
    return "The result is a tie!";
}
else if (choice1 === "rock") {
   if (choice2 === "scissors") {
      return "rock wins"
   }
}
else { // here 
   return "paper wins"
}
else if (choice1 === "paper") {
     if (choice2 === "rock") {
       return "paper wins"
     }
}
else {  // and here 
    return "rock wins"
}
}

#7

okay, so we have a function in which we are going to compare the 2 choices, and first we have an if condition to check for a tie:

var compare = function(choice1, choice2) {
    if(choice1 === choice2) {
        return "The result is a tie!";
    }
   # add the else ifs here
}

but now the interesting bit, we are going to add an else if statement to see what choice1 is:

else if (condition){

}

when we are going to add an if and else statement inside the else if to see what choice2 is, to see who won:

 else if (choice1 === "rock"){
    if (choice2 === "scissors"){
       return that rock won
    } else {
        return that paper won
    }
}

you have to follow this structure for the following few lessons to get all choices


#8

2 posts were split to a new topic: 7. choice1 is paper


#9

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