Whats wrong with my sintacs, where did i go wrong?


#1

Please guys help. I alwause get error "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 "papir wins"
}
else if ( choice1 === "paper") {
    if (choice2 === "rock") {
        return "paper wins"
    }
} else {
    return "scissors wins"
}

};


#2

Hey @devcoder47506! Im pretty sure the problem is that im preety sure you shouldnt have added the bracket at the end


#3
else if ( choice1 === "rock") {
    if (choice2 === "scissors") {
        return "rock wins"
    }
}   else {
    return "papir wins"
}

The else (and the one in the other else if) should be inside of the else if as well. Meaning it should be an exception to the if choice2 option not to the else if itself.


#4

Remove the first }


#5

Hmm, im not sure that i understand you.

This is the HINT code:

if (condition) {
  // do something
}
else if (condition) {
  if (condition) {
    // do something 
  } 
  else {
    // do something else
  }
}

I think that my else is part of my else if statment.
Can u please write the code what did u mean, it would help me a lot.

Thanks in advance.


#6

Yes the hint is correct but if you have a look at your code:

else if ( choice1 === "paper") { //open else if
    if (choice2 === "rock") { //open if
        return "paper wins"
    } //close if
} /* close else if*/ else { //open else
    return "scissors wins"
} //close else

in contrast to the hint:

if (condition) {
  // do something
}
else if (condition) { // open else if
  if (condition) { //open if
    // do something 
  } close if
  else { //open else
    // do something else
  } //close else
} //close else if

you can see that in your case the else is outside the else if and therefore it is an exception to the else if and not to the if as it is expected to be.


#7

Ok, you want to say that i have one bracket too more!
But if i erase that bracket code dont work and i cant pas to next assigment.
This code i working by codecademy.
I dont know how, i realise that i have one bracket too much but without that bracket code dont work!

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

In next asigment i correct that and it also dont work.

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 "papir wins"
    }
    else if ( choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins"
        }
        else {
        return "scissors wins"
    }
};
}

#8

  } //<------ One too many closing brackets.  In both positions
}   else {

This appears twice in your code, just get rid of the extra closing brackets and you should be good to go.