Rock, Paper, Scissors: what is wrong with my code?


#1

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

An error message says, "Oops, try again. There was a problem with your syntax." What's the problem? How can I fix it?

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


#2

check your variable used in your seconds if statement


#3

Can someone please help me figure out whats wrong. I've been re-writing the code for literally 3 hours! I keep getting an syntax error for "unexpected token else"

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


#4

There should be a hint after the instructions about syntax for an if/else statement inside of an else if statement. It goes like this:

else if(condition){
if (condition){
return code;
} else {
return other code;
}
}

The first and last curly braces encapsulate the codeblock for the else if on line 1.


#5

Thanks so much I didn't realize I was missing another set of {}


#6

Thanks! I can't believe I made such a silly mistake.


#7

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