4 ComputerCoice Part: 2 (Can somone help with this problem?


#1

Can somone help with this problem .

var userChoice = prompt("rock");
var computerChoice = Math.random();
console.log(computerChoice);
if (computerChoice <= 0.33) {
return "rock"
} else if (computerChoice <= 0.66) {
return "paper"
} else {
if (computerChoice <= 1.0)
return "scissors"
}


#2

You don't need this one:

if (computerChoice <= 1.0)

because Math.random() already makes sure that this is correct. Also your not inside of a function so using return is probably the error you're looking for. Better reassign computerChoice instead e.g.

computerChoice = "rock"

#3

I tried excluding it but it still did not work


#4

Here you go

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 for your help.


#6

I was just about to ask the same questions. Glad I found this thread. Thanks!


#7

the reason why I didn't attempt it like this is because of the ranges. wouldn't this be incorrect because .33 could fall under rock or paper?


#8

No 0.33 fulfils this condition:

if (computerChoice <= 0.33) {

and so it is rock. The paper condition would only be considered if this was false.


#9

ahh I see, thanks for clearing that up! Forgot that it goes down the conditions like that.