Unexpected token {


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-Bthev-mskY8/1/4?curriculum_id=506324b3a7dffd00020bf661


it keeps saying unexpecte token: { and ive been working on this for a while and i need a second set of eyes to help me out. 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 (chocie1 === "scissors") {
        if (choice2 === "rock") {
            return "rock wins";
        } else { 
            return "scissors wins"; 
        }
    }    

}
console.log(compare (userChoice, computerChoice));


#2

The unexpected token is in the var compare function. See the line where it is written

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

There the { is not required and that's the unexpected token. Remove it from the code. I've removed it for your convenience and added some indentation. The remaining code is exactly the same as above.

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

}
console.log(compare (userChoice, computerChoice));

#3

The problem is only in un-wanted curly braces and spelling mistake of choice as chocie in 3rd else if condition

The Correct Code is :

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") { // 1st else if condition braces start
        if(choice2 === "scissors") {
            return "rock wins";
        } else {
            return "paper wins";
        }
        } // 1st else if condition braces close
    else if (choice1 === "paper") { // 2nd else if condition braces start
        if (choice2 === "rock") {
            return "paper wins";
        } else {
            return "scissors wins";
        }
      } // 2nd else if condition braces close
    else if (choice1 === "scissors") { // 3rd else if condition braces start
        if (choice2 === "rock") {
            return "rock wins";
        } else { 
            return "scissors wins"; 
        }
    }  // 3rd else if condition braces close  

}; // Function Closes here .. Please add Semi-Colon in end of function
console.log(compare (userChoice, computerChoice));

#4

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