Syntaxerror Error: Unexpected token Else

Asked

Viewed 1,472 times

2

I am creating a game "Stone, Paper and scissors", I am at the final moment, but my code is with an error that I cannot identify.

var userChoice = prompt("Voce escolhe pedra, papel ou tesoura?");
var computerChoice = Math.random(1);
if (computerChoice < 0.34) {
    computerChoice = "pedra";
} else if(computerChoice <= 0.67) {
    computerChoice = "papel";
} else {
    computerChoice = "tesoura";
} console.log("Computer: " + computerChoice);

var compare = function (choice1, choice2){
if(choice1 === choice2) {
    return "O resultado e um empate";
}

else if(choice1 === "pedra") {

    if(choice2 === "tesoura") {
        return "pedra vence";
    }
    else {
        return "papel vence";
    }

}

else if(choice1 === "papel") {

    if(choice2 === "pedra")
        return "papel vence";
    }
    else {
        return "tesoura vence";
    };

else if(choice1 === "tesoura") {
    if(choice2 === "pedra")
        return "papel vence";
    };
};
  • What is the mistake? What you expected to happen, and what actually happens?

  • Obg by the attention, then I have to create a game "stone, paper and scissors", then by prompt I will ask the user (player) which he chooses, from cryo a Macht.Andom giving me a random value from 0 to 1, dai define a value example 0.34 = stone, Finally comes the part where the dispute takes place, where the player defines who wins or loses, used the if Else with another if Else inside, but at the time of executing the following error: "Syntaxerror: Unexpected token Else"

  • I recommend making more descriptive titles friend and only use the snippets if adding an example for execution.

  • 1

    @Guilhermenascimento Thanks for the recommendations, I’m new here, so I’m still learning.

1 answer

5

The problem seems to be here:

…
else if(choice1 === "papel") {

    if(choice2 === "pedra")
        return "papel vence";
    }
    else {
        return "tesoura vence";
    };

else if(choice1 === "tesoura") {
…

If you pay attention, you never close the else if(choice1 === "papel") { - in the else if(choice1 === "pedra") { just above you put the }, but on that not.

(The ; in the }; is unnecessary, but it makes no difference to your code.)

  • It seems to be this same +1, maybe I could recommend Switchs and callbacks (I wouldn’t need examples on top of the code, just links to study, because in my view you already answered the question very well) :)

  • It is an option, but note that strange things happen if the user plays e.g. PEDRA or tigre... to fix this would be to rewrite the code, essentially; I find it precious to put the switch (…) {…} without making these adjustments.

  • The idea was not to add this to the code, just to indicate good readings for other practices, not linked to codes, but to "author learning" about ifs, Switchs and functions. But it can really confuse you. Better leave as it is :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.