Computer Choice: Part 2 We have computerChoice but it now equals a random number between 0 and 1. We need to somehow translate this random number into a random choice of rock, paper, or scissors. How do we do this?!


#1

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

var computerChoice = Math.random();
console.log(computerChoice);

if (computerChoice < '0.33' && computerChoice > '0') {
console.log("Rock!");
}

else if (computerChoice < '0.66' && computerChoice > '0.34') {
console.log("Paper!");
}

else if (computerChoice < '1' && computerChoice > '0.67') {
console.log("scissors!");
}
else {
console.log("result ");
}


#2

Well you're code almost fits the purpose you just need to assign the value instead of console.logging it:

computerChoice = "Rock";

not

console.log("Rock!");

Also 2 more details. First if you want to compare numbers, use numbers not strings:

if (computerChoice < '0.33' && computerChoice > '0') {

it probably works with '0.33' but as you're interested in the number 0.33 why not just use this. Also when using strings and numbers you might get into trouble when you use the + operator because "5"+5 is "55" and not 10. So here it works but rather be careful with it or just use numbers when you want to use numbers.
Second you could get rid of some conditions if you think about the boundary conditions for example are there even cases left where the else is triggered? Should that be the case? Could Math.random() be below 0 or above 1?


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.