8/9 unexpected token else


#1

What's wrong with my code? Thanks

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


#2

You have one extra bracket with one extra semicolon.

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";
        }
    }; // This line has no purpose
    } else if(choice1 === "scissors") {
        if(choice2 === "rock") {
            return "rock wins";
        } else{
            return "scissors wins";
        }
    }
}

#3

also your very last } is the only one that should have a ;


#4

Thanks a lot guys you were all a remendous help! :smile:


#5

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

can anyone help?? It keeps saying parser error


#6

Lets start at the top of your function. Your code:

var compare = function (choice1, choice2){
    if (choice1 === choice2){
        return "The result is a tie"; ///good up to this point
    } else if(choice1 === "rock") {
        return "rock wins"; ///here we have a problem.  You are saying anytime choice1 is rock to return "rock wins"
        ///You should have an if statement under your else if that looks something like:
        if (choice2 === "paper" {
            return "paper wins";
        } else{
        return "scissors wins"; ////This line should say "rock wins"
    }
    }
    } if (choice2 === "scissors") { ///the "}" at the start of this line is closing your function.  Also you should have an else if here with a choice 1 not an if choice 2
        return "rock wins";
    } else {
        return "paper wins";
    }
    } else if(choice1 === "paper") { ///This whole section looks wonderful.  Use it to check the rest of your if, else if, else statements
        if(choice2 === "rock") {
            return "paper wins";
        } else{
            return "scissors wins";
        }
    }
};
userChoice(scissors) ///once again we have problems Houston
computerChoice(rock) /// these two lines should not exist.

You wrote code at the beginning of the exercise that gathers the choices of both the computer and the user. All you need to do here is:

compare(userChoice, computerChoice);

Good luck and keep coding. Any more questions feel free to ask.


#7

I get the same error with mine:

var compare = function(choice1, choice2)
{if (choice1 === choice2)
{return "The result is a tie!";}
else if (choice1 === "rock")

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


#8
else {return "scissors wins"}}} ///delete one "}" from this line

else {return "rock wins"}}}} /// delete at least 1 of the "}" here
/// 2 of them if you have a "}" closing your function elsewhere
/// in your code not shown here.

else if (choice1 === "rock") ///delete this last line entirely.

Good luck. A helpful hint though. Using the nesting method in your code is very helpful when it comes to counting your opening and closing {} . While nesting will cause your code to be more spread out it will also make it a little easier to read and debug
Example:

else if (choice1 === "scissors") { /// opens the else if
    if choice2 === "paper") { /// opens the if
        return "scissors wins";
    } /// closes the if
    else { /// opens the else
        return "rock wins";
    } ///closes the else
} /// closes the else if
}; /// closes the function

Hope this all helps