4. Computer Choice: Part 2 help!


#1


Exercise

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

#2

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!


4. Computer Choice Part 2
#3

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


#4

Hi here

else if (computerChoice <= 0.66)

Change it like that

else if (computerChoice > 0.66)

#5

You should change else if like num 2.

@wizmarco note is correct...


#6

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.


#7

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...


#8

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:


#9

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);

#10

Add ";" to the end...

Remove ";" from the end...


#11

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

#12

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');
};


#13

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.


#14

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


#15

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


#16

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?