4/9 Computer Choice: Part 2


#1


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


Just started learning javascript a couple hours ago, got stuck on this project where the error was 'SyntaxError: Unexpected token {'


var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
console.log(computerChoice);

if (computerChoice >= 0 && computerChoice <= 0.33) {
    computerChoice = "rock";
} 
else if (computerChoice >= 0.34 && computerChoice <= 0.66) {
    computerChoice = "paper";
} 
else (computerChoice >= 0.67 && computerChoice <= 1) {
    computerChoice = "scissors"
}


#2

instead of:

else (computerChoice >= 0.67 && computerChoice <= 1) {
computerChoice = "scissors"
}

try:

else {
computerChoice = "scissors";
}

Noticed that I deleted the last condition and added ";" after the re usable code. It should work with that..


#3

I too am tearing out my hair on this one. I am swearing at my computer! I keep getting syntax error. Help! :slight_smile:

Here is my code.

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


#4

Got there! Good old internet.

Worked it out! Woo hooo! This worked for me:

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


#5

thanks rodrigomeono! could you explain what was the issue? was it because I was lacking a semi colon at the end?


#6

You are welcome Michael I'm glad it worked! It was not working before because the "else" statement can not contain a condition, if you need another condition after your "if" statement you can use "else if". Also a semicolon should always end a line of code between { }.