8. What if choice1 is scissors?


#1

Hi, I'm having trouble with exercise 8.
I can't seem to find what's wrong with my code, the error message says: "Unexpected token else".

var userChoice = prompt("Do you choose rock, paper or scissors?");
console.log("User Choice: " + userChoice);

var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} console.log("Computer: " + computerChoice);

function compare (userChoice, computerChoice) {
    if (userChoice === computerChoice) {
        return "The result is a tie!";
}

else if (userChoice === "rock") {
    if (computerChoice === "scissors") {
        return "rock wins";
}

else {
return "paper wins";
}

else if (userChoice === "paper") {
if (computerChoice === "rock") {
    return "paper wins";
}
else {
    return "scissors wins";
}
else if (userChoice === "scissors") {
    if (computerChoice === "rock") {
        return "rock wins";
    }
    else {
        return "scissors wins";
    }
};
console.log(compare);

Thank you.


#2

in the past few exercises you have built 3 else if which checks for choice:

else if (choice1){
  
}

inside else if you nested an if and else to check for choice2:

else if (choice1){
    if(choice2){

    } else {

    }
} // closes else if
// next else if

You need to close else if before moving on to the next else if. Walk through you code to see you got all curly brackets right

If you need more help, post an updated version of your code


#3

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