Javascript, Rock+paper+scissors


#1

Greetings,

I am stuck at this section, i believe my answer is correct & about to lose my marbles. Seriously!!!

below is my code: and this is the error message SyntaxError: Unexpected token else

    /*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 (choice2==="scissors"){
            return "rock wins";
            }else{
                return "paper wins";
            }
            else if(choice1 === "paper"){
                if (choice2 === "rock"){
                    return "paper wins";
                    else{
                        return "scissor wins";
                    }
                }
            }
    };

#2

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

Here you has an else if following an else, which makes no sense as else deals with anything left so there is no need or possibility for a more specific else after this. Also return is a keyword and does not require () around the value it returns.


#3

Figured it out, Thanks.


#4

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