8. What if choice1 is scissors - SyntaxError: Unexpected token {


#1

Can someone explain to me where I am going wrong. It comes up with SyntaxError: Unexpected token{
I think the mistake is more towards the compare variable and the if else statements.

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

compare(userChoice,computerChoice);

I have tried to find the answer within other threads but still can't get to the root of the problem!

Thank you very much in advance!


#2

You have 11 } and only 10 { Seemingly if you take one out at the end you should be fine


#3

Thanks, now its just trying to figure out where haha!


#4

Take one of these out?


#5

I tried taking out one of them, same SyntaxError message. I am trying to pair up the opening and closing {} to figure out which one the culprit is.


#6

Yeah solving it the old fashion way haha


#7

Found the bugger - I was actually missing a closing parentheses behind the "scissors" in the else if argument.

Thank you for your help though! :slight_smile:


#8

Hmmm interesting yeah when I saw your error I stopped looking once I counted the { } but that is great you solved it basically on your own!


#9

My problem is supposedly an unexpected <

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
console.log(computerChoice);
if (computerChoice ==< 0.33) {
console.log("rock");
} else if (computerChoice >== 0.67) {
console.log("Paper");
} else {
console.log("Scissors");
}


#10

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