#7 unexpected else token?


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-Bthev-mskY8/1/3?curriculum_id=506324b3a7dffd00020bf661#


i cant figure out why its giving me an unexpected else token. can someone point out why this isnt working for me?


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";
        }
    }
    };


#2

figured it out this time


#3

You posted that just as I was about to reply :stuck_out_tongue:

For those who don't see it:

`
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";
    }
    //Extra } needed here.
else if(choice1 === "paper") {

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

`


#4

Instead of making a new thread I've decided to post here. I still can't figure out why it is giving me a "SyntaxError: Unexpected token else"
I would really appreciate it if someone can share some light, because I can't get my head around this.

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";
    }
};

#5

I left comments in your code. At the end you still need to close the compare function.


#6

Thank you very much biff75!


#7

Next time, please make a new thread- it's cleaner.


#8

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