jjdouble000
jjdouble000

Reputation: 93

Rock Paper Scissors Game Error, Javascript

I keep getting this error from the console:

SyntaxError: expected expression, got keyword 'else' else if (choice2 === "paper") {

This is my code

var user = prompt("Rock, Paper, Scissors?");
var computer = Math.random();
if (computer < 0.34) {
    computer = "rock";
} else if (computer <= 0.67) {
    computer = "paper";
} else {
    computer = "scissors";
} console.log("computer: " + computer);

var compare = function (choice1, choice2) {

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

}
else if (choice2 === "paper") {
    if (choice2 === "rock") {
        return "paper wins";
    }
    else {
        return "scissors win";
    }
}
}
compare(user, computer);

Upvotes: 1

Views: 57

Answers (1)

Jesse
Jesse

Reputation: 2830

Well.

You end the compare function on line 25 or so and then start up another else. So that is not good, but it is just a duplicate curly brace, so when removing it we still get the error. This is because you are calling an else if after an else.

When we move the else to to under the else if and remove the duplicate curly brace, we get some code that actually runs

var user = prompt("Rock, Paper, Scissors?");
var computer = Math.random();
if (computer < 0.34) {
    computer = "rock";
} else if (computer <= 0.67) {
    computer = "paper";
} else {
    computer = "scissors";
}
console.log("computer: " + computer);

var compare = function(choice1, choice2) {

    if (choice1 === choice2) {
        return "Tie";
    } else if (choice1 === "rock") {
        return "Rock wins";
    } else if (choice2 === "paper") {
        if (choice2 === "rock") {
            return "paper wins";
        } else {
            return "scissors win";
        }
    } else {
        return "paper wins";
    } 
}
compare(user, computer);

Hope this helps. Also it makes it easier to debug (see the extra curly braces) when you use a tool to beautify your code a bit, I use -> http://jsbeautifier.org/

Upvotes: 1

Related Questions