Build "rock, paper, scissors" part 7


#1


I dont understand where I am going wrong, whenever i try to rune the code it says "unexpected token else" please help


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


#2

a normal if statement:

if (condition){

}

in your case, two if statements are missing {

If you need more help, post an updated version of your code


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

(end);

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

else

{
return "scissors win"
}

I have added the two "}" that i believe you were talking about but it still says "unexpected token".
I am so confused right now


#4

Got firefox installed? Open up the scratchpad (shift f4) and paste your code in there for a better error message


#5

no, i said opening curly brackets ({) where missing.

so, we build an else if statement to check for choice1:

else if(choice1){

}

then inside the else if we nest an if and else to check for choice2:

else if(choice1){
   if(choice2){

   } else {

   }
}

you should make sure you follow the same structure


#6

awesome :slight_smile: i should have known this earlier, does it work as well for jquery?


#7

Seems like it executes it in the page it was opened in, so I don't see why not


#8

chromium-based browsers have something similar, it's just slightly more inconvenient to find it's something like console->sources->snippets->new-snippet


#9

Give me firefox any day, but i am afraid we get a bitt off topic


#10

I have modified my code using guidance from both yourself and the internet, however I still cannot get rid of the syntaxerror on line 28, please help.

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

#11

no closing curly bracket to close/end your function


#12

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