SyntaxError: Unexpected token else


#1


Rock, Paper, Scissors game


SyntaxError: Unexpected token else


/*var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} console.log("Computer: " + computerChoice);*/

var compare = function(choice1, choice2)
{
    if(choice1 === choice2)
    {
        return "The result is a tie!";
    } else if(choice1 === "rock")
    {
    } else if(choice2 === "scissors")
    {
        return "rock wins";
    } else
    {
        return "paper wins";
    }else if (choice1 === "paper")
    {
        if(choice2 === "rock")
        {
            return "paper wins";
        }else
        {
            return "scissors wins";
        }
    }
}


#2

HI you close this else if statement earlier

else if(choice1 === "rock")
    {
    }  <-- #remove the }

#3

I removed the bracket and still getting erros :confused: thank you for you help


#4

this part of your code you didn't close the else if statement

else if(choice1 === "rock"){
    else if(choice2 === "scissors"){
	return "rock wins";
    } 
    else{
        return "paper wins";
    }
} <-- #add this one }

then this line

else if(choice2 === "scissors"){

its should be a if statement instead of a else if statement


#5

Perfect! The odd thing about this lesson was that I feel i could have figured it out on my own but the program expects a certain format. Thank you!


#6

what do you mean by ...the program expects a certain format


#7

Meaning the CodeAcedamy lessons usually only except a specific format. An example would be - I would structure my if statements different then what they wanted me to. I would not say my way is better but i have my own programming style and being forced to completed it in their desired manner can be tough for me to debug. This is not a bad thing and I 100% understand why having a structured and controlled lesson design will help with forums like this (since it only accepts 1 way). I did not like this set up for the nested-if's. Nothing wrong just a personal preference I guess. Does that make sense?


#8

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