7/9 SyntaxError: expected expression, got keyword 'else'


#1

this is my code probably not very neat but this is it and id appreciate some help as im sure its right, obviously it cant be but yeah.

can someone help me pleasee lol.

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

#2

Seems to be ok so far, what is the error message and what is the rest of the code?


#3

ok so i just added an if statement and its now telling me that rock wins instead of 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 === " ") {
     }
     
     else if(choice1 === "paper") {
         if(choice2 === "rock") {
             return "paper wins" 
         }
         else { 
             return "scissors wins"
         }
     }
             
};

#4

Ok the idea is that you nest the if/else inside the else if instead of just leaving the else if useless:

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

Now this means something like if choice1 === "rock" and choice2 === "scissors" then "rock wins" or if choice1 === "rock" and choice2 is not scissors then "paper wins" as this is the only chance left as rock is covered by tie and scissors by the case directly in front of this. So if you structure this case like you did for the next then it should work. Also you're then still in the "else if"-chain and don't need a useless if to start an else if again.


#5

allright thanks for your help man :smile:


#6

I have the same error message (syntaxerror:expected expression, got keyword 'else') and I'm still confused by your explanation. I thought that we needed to write another else if statement where paper wins and return with paper wins else scissors win according to the instructions. Can you please explain to me what I'm doing wrong, I tried following the instructions based on the previous exercise similar to this one? My code is posted below.

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


#7
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
} // else if 
} // function

as far as I can see your closing the function to early so that the else if part is now outside but else statements can't be without if's so you get the error.


#8

please help i am getting the same error

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

#9

Is the lack of {} intentionally? Because here it went wrong:

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

the problem is that the else should belong to the choice2 option like it does in your second case:

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

but currently the else ends the whole condition meaning anything that is not tie or choice1 being rock results in paper wins. So as said you might want to fix it according to your other case.


#10

Bonjour j'ai le même problème je n'arrive pas à valider le cours car selon le script j'ai une erreur de syntaxe. Pourtant, quand je vérifie toutes les accolades sont bien fermées. Quelqu'un pourrait m'aider ?

    var choixUtilisateur = prompt("Choisissez-vous pierre, feuille, ou ciseaux ?");
    var choixOrdi = Math.random();
    if (choixOrdi < 0.34) {
        choixOrdi = "pierre";
    } else if(choixOrdi <= 0.67) {
        choixOrdi = "feuille";
    } else {
        choixOrdi = "ciseaux";
    } console.log("Ordinateur : " + choixOrdi);
    var comparer = function(choix1, choix2)
    {
    if (choix1 === choix2)
    {
        return "Egalité !";
    }
    else if(choix1 === "pierre")
    {
        if (choix2 === "ciseaux")
        {
            return "pierre gagne";
        }
        else
        {
            return "feuille gagne";
        }
    }  
    else if(choix1 === "feuille")
    {
        if (choix2 === "pierre")
        {
            return "feuille gagne";
        }
        else
        {
            return "ciseaux gagnent";
        }
        else if(choix1 === "ciseaux")
        {
         if (choix2 === "feuille")
            {
                return "ciseaux gagnent";
         }
        else
        {
            return "pierre gagne";
        }
      }    
    }    
    };
    comparer(choixUtilisateur, choixOrdi);

#11

Sry my french is rusty, I can read a bit but writing is more difficult :frowning:
You forgot a closing ) here:

 else if(choix1 === "feuille")
    {
        if (choix2 === "pierre")
        {
            return "feuille gagne";
        }
        else
        {
            return "ciseaux gagnent";
        }
        // } <-- here
        else if(choix1 === "ciseaux")

So the else if for "ciseaux" is nested and that leads to else if following on else which is impossible.


#12

Hello. Please, help me out, I broke my head on this "else" error:

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

#13

Hi this part you should remove the ; after the if conditon

if (choice2 === "rock"); <== this one

#14

@wizmarco is right and faster :slight_smile:
Here is a longer explanation about what happens with the semicolon: