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?!


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

var computerChoice = Math.random();

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

else if (computerChoice < '0.66' && computerChoice > '0.34') {

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


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

computerChoice = "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?


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