Getting Between .67 and .34


I noticed that people are inputting computerChoice < .34 for rock, and then inputting computerChoice <=.67 for the paper option. This seems incorrect to me, because something that is less than .34 will also be less than .67, so simply using <=.67 does not seem right. Anyhow, I found that using && allows you to truly input a variable as being between two values. Just thought I'd share!

var userChoice = prompt("Do you choose rock, paper or scissors?")
var computerChoice = Math.random();
if (computerChoice < .34) {
    computerChoice = "rock";
} else if (computerChoice <=.67 && computerChoice >=.34) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
} console.log("Computer" + ":" + computerChoice);


Yep! I just assumed that was the correct way to do it. Although for "rock" I put >=.33.

Then when I submitted and went to the next turn the site changed my answer up to be more efficient to them I'm assuming. Either way it worked and that's the important part to me. :slightly_smiling:


Yes this would be the combination to build an "in between" condition:

A < x && x < B

So why isn't && introduced yet to build one?

Well you don't need it in this case. First of all the Math.random deals with some of the boundary conditions
(0 <= x < 1), so you don't need:

if (computerChoice > 0 && computerChoice < .34) {

but can just use:

if (computerChoice < .34) {

because Math.random is always bigger than or equal to 0.
And the other thing that helps is how conditions work. Think about it, if you have:


when will you run code1?

when the condition is true.

when will you run code2?

You only even consider it when the condition is false.

So as else if is just a fancy way to write:


without the additional {}, you again only enter the else branch if the if branch was false.

So when is your if branch false?

When computerChoice < .34 is false or written differently computerChoice >= .34. So you already have this condition checked implicitly because this being true is the only way to even get to the else if in the first place, so in the else if you just need to check the second condition.

What's also interesting is that most people use 0.33 or 0.34 and stuff like this where probably the easiest way to split this into 3 pieces would be 1/3, 2/3 wouldn't it?

Hope this explains this a bit better why using computerChoice <=.67 is totally sufficient.

4.Computer Choice Help please!

Thanks for the info! I just swear I never got "rock" to appear when I had the "paper" else if set at <=.67


Maybe just bad luck :wink: