SyntaxError: Unexpected token else


#1


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


else if(choice1 === "paper") {
    
    if(choice2 === "rock") {
        
        return "paper wins";
    }
    
    else
        {return "scissors wins";}
        
        
       }
    }
    
else if(choice1 === "scissors") {
        
        if(choice2 === "paper") {
            
           return "scissors win";
        }
        
        else { 
            return "rock wins";
        } 
    }
    }
    
     compare(userChoice,computerChoice);


#2

When is else allowed?

As an optional second part of an if-statement, right?

So if you have an "unexpected else", then one of them is probably not in such a position.

Note that your indentation doesn't match your curly braces, this might have caused you to misread your own code and put something in the wrong place. So you should probably start by fixing that, and while doing so you'll probably also find the bad else.


#3

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") {

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

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

if(choice2 === "rock") {

    return "paper wins";
}

else
    {return "scissors wins";}


   }  _// Don't quote me on this, but isn't this one too many? o.O_
}

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

    if(choice2 === "paper") {

       return "scissors win";
    }

    else { 
        return "rock wins";
    } 
}
}

 compare(userChoice,computerChoice);

#4

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