Lesson 4 - omputer Choice - Part 2


#1

Good Morning coders,

I'm having some problems with my syntax on the below code:

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

};

Any help will be appreciated.


#2

Math.random is just a function call not a function so you don't need the {} around the following lines. Also be careful with conditions after curly brackets when it comes to condition, here is a longer explanation:


#3

So I amended the code as follows;

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

But I'm still getting the syntax error?


#4

You still have these semicolons here:

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

var computerChoice = Math.random(); 

 if (computerChoice < 0.33) {
    computerChoice = "rock" }; // <--here

else if (computerChoice < 0.66) {
    computerChoice = "paper" }; // <-- here

else { computerChoice = "scissors" }; // <-- here

if you want to place semicolons than here would be the better place:

computerChoice = "scissors"; // <-- here

Have you read the link?