Why don't you need last "else" statement for final "rock, paper, scissors" part 8 or 9?


#1

at the very end of the "rock, paper, scissors" your code looks like this:

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

the compare function basically contains if/else if/else if statements, with if/else statements nested inside the if/else if parent statements. my question is why doesn't the if/else if/else if parent statements require a final else statement? how can the final parent statement

  } else if (choice1 === "scissors"){

end in else if instead of else? does this have something to do with the nested else statements?

sorry if i'm being unclear and or using incorrect syntax.

thanks!


#2

if statements do not ever require an else statement to follow them.

if(true){
    console.log("something");
}
#this is still proper syntax, even though it is a solitary if statement

#3

No it's not about the nested context. It's just that if could stand alone. Either the code inside is executed or it is not. Only else cannot stand alone as "do this instead" makes no sense without an initial statement.