New coder here, help me fix this code


#1



I'm on this excercise https://codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/1/4?curriculum_id=506324b3a7dffd00020bf661#


I'm getting this error SyntaxError: missing before statement


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


#2

Hey,

I would recommend have a look at what the syntax should look like, and then have a look at your syntax :slight_smile:
syntax should look like:

if (condition) {
  // do something
}
else if (condition) {
  if (condition) {
    // do something 
  } 
  else {
    // do something else
  }
}

#3

Try using JSHint.com

Ive just had a look and can see what appears to be your problem, but im a noob so i wouldnt like to give you the wrong advice...


#4

Here's an updated version with what the syntax should be I hope

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


#5

Hi this part you closed the compare function before and you should add the open { of the if statement and remove the ; after the closing else statement

else if(choice1 === "rock") {
    if (choice2 === "scissors") { <== add one here
        return "rock wins"
   }
} <== remove this one
    else {
        return "paper wins";
    }
}; <= remov the ;

the same here you shoudl pen the if statement and add one { and at the end for closing the compare function

else if(choice1 === "paper") {
    if (choice2 === "rock") { <= add one here 
        return "paper wins";
    }
    else {
        return "scissors wins";
    }
}
} <== this one too

#6

Alway's double check your code.


#7

Thanks Marco, you really are a wiz.