4. Computer Choice: Part 2 help!

<Below this line, add a link to the exercise!>
Exercise
<Describe the problem. Which error message are you seeing?>
I’m stuck on 4. Computer Choice: Part 2. I keep getting a syntax error message. At first it said it needed semicolons, then I added some, but now it’s just giving me a general syntax error message?


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 >= 0.67) {
    computerChoice = "scissors";
};


1 Like

You should have three choices:

  1. if (computerChoice <= 0.33)
  2. else if(computerChoice > 0.66)
  3. else

!!! Don’t use ANY logic for else statement!

2 Likes

I got rid of the logic, but it’s still giving me a syntax error??


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

1 Like

Hi here


else if (computerChoice <= 0.66)

Change it like that

else if (computerChoice > 0.66)
3 Likes

You should change else if like num 2.

@wizmarco note is correct…

2 Likes

That line is fine. The problem is on the previous line where you ended the entire if statement by putting semicolon after the if block:

};

You’ve done it again after your else if block.

2 Likes

Can i ask why computerChoice <= 0.66 is correct?

!!! Oh, I got it! It’s because of the importance in JS, But what i said is fine too…

1 Like

Yes it is, both ways work. In that case you may want scissors to be the choice for that. – if you want to stick to the plan :slightly_smiling:

2 Likes

Seems to be a big of a disconnect between “always put semi colon ‘;’ at end of your line of code like full-stop/period” versus exercises like this… where my code only worked if i removed all the semi colons/ ‘;’ apart from the last one.

var userChoice = prompt("Do you choose rock, paper or scissors?")

var computerChoice = Math.random();

if (computerChoice <= 0.33) {
    computerChoice = "rock"
}

else if (computerChoice > 0.66) {
    computerChoice = "paper"
}

else {
    computerChoice = "scissors"
};

console.log(computerChoice);
1 Like

Add ";" to the end…

Remove ";" from the end…

1 Like

I’m not sure where it says “line”, but it shouldn’t, it should say “statement”. If you are writing something longish, like an if/else statement, you don’t want to put semicolons at the end of each line because they signal the end of the statement. You can put them at the end of the statements within the blocks , { }, but don’t break up the whole if/else statement by adding them in other places.

sample if/else statement:

if (condition) {
    // if true block
    statement;
    statement;
} else {
    // else block
    statement;
    statement;
}
2 Likes

Hello, here’s something that worked for me :

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

console.log(computerChoice);

if (computerChoice <= 0.33) {
console.log(‘rock’);
} else if (computerChoice <= 0.34 <= 0.66) {
console.log(‘paper’);
} else {
console.log(‘scissors’);
};

2 Likes

That should be:

} else if (computerChoice <= 0.66) {

You’ve already diverted all numbers <= 0.33 so you don’t need to worry about those again.

1 Like

Thank you albionsrefuge ! I was sure there was a sampler way to do it

2 Likes

2 posts were split to a new topic: I keep getting illegal return statement

I have two questions about this:

  1. Why does using logic in else make it incorrect syntax?

  2. Why do you have > in the else if line when the instructions define it as between 0.34 and 0.66?

1 Like