8/9 - Rock, Paper, Scissors - Can't see what is wrong?!


#1

Please help - I just get "SyntaxError: missing before statement"

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);

compare = function (userChoice, computerChoice) {

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

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

}

else (userChoice === "rock") {
    if (computerChoice=== "paper") { 
        return "Paper wins!" 
        }
    else  { 
        return "Rock wins!"
        }
}
}

#2

You use a condition after else which is not allowed as else is the default case which does not require a condition. Also why do you use user and computerChoice instead of choice1 and 2?


#3

You should write var compare = function(userChoice, computerChoice)

Also after return put semicolon ;

example

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 if(choice1 === "scissors"){
        if(choice2 === "rock"){
            return "rock wins";
        }
        else{
            return "scissors wins";
        }
    }
};
compare(userChoice, computerChoice);

#4

Thanks everyone :raised_hands: