Rock Paper Scissors 1 step from finishing


#1



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


If I understand the error correctly I've missed a ';' somewhere in my code but I've gone through it multiple times and compared it side by side to others' code yet I can't find the missing semi-colon. Any help would be great, thanks!

SyntaxError: Expected ';'


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

compare(userChoice,computerChoice);


#2

you wrote only else instead of else if

else if(choice1 === 'scissors') {
if (choice2 === 'rock') {
return 'rock wins';
}
else {
return 'scissors win';
}
}
};


#3

Thank you so much! So does "if/else if/else" only work in groups of 3? If you have a continuous if statement you keep using "else if" and end it with "else if"?


#4

@hapyhapa You use else if when you still have code remaining that you want to run in only certain conditions (not fulfilled by above if and else ifs).

else is used if you want to run some code in a situation not caught by any of those (like a "default"), i.e. no else is necessary if you don't want your program do do something for any remaining conditions.

I believe you can use as many else ifs before the else as you like, though it may not always be the most effective way. :slight_smile: