Where is "unexpected 'else'"?


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-Bthev-mskY8/1/4?curriculum_id=506324b3a7dffd00020bf661#
What if choice1 is scissors? (4.8)

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==="Rock") {
            return"Rock Wins!"
        }else {
            return"Scissors Wins!"
        }
        
    }

};
userChoice(choice1);
computerChoice(choice2);


#2

  else if (choice1==="paper") {
        if (choice2=="rock") {
            return"Paper Wins!"
        }else {
            return"Scissors Wins!"
        }
       //here you forgot to close else if

next

userChoice(choice1);     //wrong
computerChoice(choice2);  //wrong

you have to call the function by function name and give the arguments inside the parameter.so here your function name is compare.so call the function like

console.log(compare(userChoice,computerChoice));

#3

Thank you @rcodeman !! Very helpful!! I just couldn't find that one bug with the "}"!!


#4

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