Computer choice Part 2


#1

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

I cannot see the problem for the life of me and have tried several variations. Please help!


#2

No semicolons after conditional statements see this link for an explanation:


and no condition after else.


#3

I'm still having trouble it's now saying illegal return statement.

I've removed the semicolons from the conditional statements and the condition from the else but still having trouble.

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


#4

The semicolons are still there and yes return is a keyword that is only allowed in function, so as there is no function there should not be any return. Afaik the instructions want you to assign a new value to computerChoice and not to return it.


#5

Thanks I got it now.


#6

But Guyzz, How to use the between operations in if statement?

Is my approach correct??

`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,computerChoice<=0.66) 
{
    computerChoice="paper"
}
else(computerChoice>=0.67,computerChoice<=1.00)
{
    computerChoice="scissors"
}`

Can I use for the if conditions??


#7

Instead of a comma to join the parameters use && - that means "and" and will check that both the conditions are true.


#8

okay so I have:

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


Can anyone help me understand what I did wrong? I looked at everyone else's responses and modified my result through that but It still says that there was a problem with my syntax.


#9

Your condition is actually correct the problem is that you messed up with the () and {} its:

if(condition){
    code
}
else if(other condition){
    code
}
else [if(yet another condition)]{
    code
}