Syntax errors with code


#1

hey everyone below is my code and i wanna know what i did wrong. it keeps giving me the error message syntax and also tells me in the text box that there is a problem with 'else' please help.

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";
    }
}
compare(userChoice,computerChoice)

#2

There are two syntax errors here.

1) There is a semi-colon after if(choice1===choice2) instead of a opening curly brace {.

2) There is no closing curly brace for the compare function declaration. If you add another closing curly brace } directly above the line compare(userChoice,computerChoice), you should be able to run this.

It's easier to spot these syntax errors if you're very careful about indentation. The else if(choice1 === rock) should be indented the same amount as the if(choice1===choice2) {, which will also mean indenting the code within the else if block some more. If these lines are correctly indented, it's easier to spot the missing closing curly brace at the end of the function declaration.


#3

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

}
}

compare(userChoice,computerChoice)
okay i think i did what you said but its still saying 'else error'


#4

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