8/9 Help Please SyntaxError: Unexpected token '{'. Parse error


#1

Someone please tell me what I'm doing wrong here. When I hit Save & Submit Code it just gives me a SyntaxError: Unexpected token '{'. Parse error.

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

compare(userChoice, computerChoice);

#2

I find two error on your code, the first one is [ else(choice1 === "scissors") { ] replace it by this [ else {(choice1 === "scissors") ]. The second is, you put a semicolon at the last curly bracket. Just remove those and then run your code.

Anyway, these is edited code of yours:

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

#3

Thanks for the input, but it still gives me the same error after doing what you told me. I added an opening bracket after else and deleted the semicolon after the last bracket. Could it be a glitch or bug issue?


#4

Ok I'd have said that you better make it an else if (because with an else the (choice1 === "scissors") is pointless ):

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

And the semicolon at the last curly brace is ok that is the same syntax as var name = value; (just a bit longer). But anyway your suggestion should have already fixed the first one and the second one is not that relevant. @iwant2code5891 what error is it that you know get and how does your new code look like?