4/9 unexpected token else


#1
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
console.log(computerChoice);
if(computerChoice <= .33){
    compterChoice = "rock";
}
if else(computerChoice > .34 <= .66){
    computerChoice = "paper";
}
else {
    computerChoice === "scissors";
};
console.log(computerChoice);

//the console is logging SyntaxError: Unexpected token else

#2

When the console comes to the 7th line, it imediately thinks that there is a new if/else statement because the line starts with an if. It then discovers that the if is followed by an else, not a condition. If you start the line with an else, the console wil know that you want it to continue the if/else statement. After the first else, attach an if, so it knows there is a new condition coming.

I would also advise removing the semicolon on your last bracket, as semicolons on brackets somehow often ends with errors

Hope this helps :smile:


#3

Way too many problems sorted

single = instead 3 ===
if else instead else if or elif
(computerChoice > .34 <= .66) whereas JS doesn't have in between symbol + your comparison is also un-readable by human. computerChoice should be in between.
; after else }
why print console.log again in the last line?

Please check out my full solution till 8/9 below:

    var userChoice = prompt("Do you choose rock, paper or scissors?");
    var computerChoice = Math.random();
    console.log(computerChoice);
    if (computerChoice >= 0 && computerChoice <= 0.33) {
        computerChoice="rock";
    }
    else if (computerChoice >= 0.34 && computerChoice <= 0.66) {
        computerChoice="paper";
    }
    else {
        computerChoice="scissors";
    }
    var compare = function(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 === "paper"){
                return "scissors wins";
            }
            else {
                return "rock wins";
            }
        }
    };
    compare(userChoice, computerChoice);

Hope this helps :sunglasses:


#4

Here is something about the use of if/else and where else if comes from:


And this about why in between statements don't work like this:
https://www.codecademy.com/forum_questions/51fc4178f10c60f4eb001233#answer-529f101f7c82ca3ce100308d

@micromaster14720 Pretty good but I guess elif is more python like syntax the else if is probably the version javaScript prefers :smile:


#5

Thanks for the feedback.