Syntax error


#1

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

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

compare(userChoice, computerChoice);


#2

Does an else statement take a condition?


#3

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

Can anyone help me.This code returns syntax error .


#4

I think there's a spelling error somewhere.


#5

Hi. I keep getting a Syntax Error with this code. I eventually copied and pasted the "help" dialogue (for the first section "choice1 === rock") and it still returned the error. I proceeded anyway with the next two (scissors and paper) and am still getting the error. I'm following the format exactly from the help section. I can't finish this section now. Please help, what am I doing wrong!

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

}


#6

Did you close your function?


#7

Above is my exact code. If I didn't close it, how should I? I thought it was "closed" with the curly bracket.


#8

I figured it out......


#9

How do you solve it?where is the problem?

I encontered the same trouble.

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, chice2) {
if(choice1=== choice2) {
return "the result is a tie!";
}
};


#10

A spelling error. found it. Thank you,


#11

Thank you! That helped me lots!!


#12

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