Isn't 0.34 and 0.67 a bad idea..?


#1

The instructions tell me to do this:

if (computerChoice < 0.33) {
computerChoice = "rock";
} else if (computerChoice > 0.34 && computerChoice < 0.66) {
computerChoice = "paper";
} else if (computerChoice > 0.67) {
computerChoice = "scissors";
};

But if math.random() returns, say, 0.337195, it does not fall under any condition. console.log(computerChoice) would then simply print 0.337195. Shouldn't this be better?

if (computerChoice < 0.33) {
computerChoice = "rock";
} else if (computerChoice > 0.33 && computerChoice < 0.66) {
computerChoice = "paper";
} else if (computerChoice > 0.66) {
computerChoice = "scissors";
};


#2

Indeed you are right. I would also continue the idea in the case where computerChoice is EQUAL to 0.33 or 0.66. Then again these cases are not considered in your code. I would write:

if (computerChoice <= 0.33) {
    computerChoice = "rock";
} else if (computerChoice > 0.33 && computerChoice <= 0.66) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
};

adding two <= for 0.33 and 0.66 in the two first if / elseif. Then if computerChoice = 0.33, we have "rock" and if computerChoice = 0.66 we have "paper". I think that it is also better to end a if/elseif by a default else in order to have an exit door.


#3

You can leave out that part of the 2nd condition.

By using else if instead of just if the 2nd condition is only checked if the previous condition was false. Therefore it is implied that computerchoice has to be >0.33 otherwise the 2nd condition can't be checked.


#4

Haha you're right. We're technically talking about a very, VERY low chance here. If I'm correct, Math.random() generates 16 decimals so the odds would literally be 1 in a quadrillion of hitting exactly that. Good call nonetheless. Even the tiniest flaw is still a flaw.