Why does it give "scissors" when it should give "paper"?


#1

https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/0/4?curriculum_id=506324b3a7dffd00020bf661#

Actually, it gets passed and I can continue to next lesson, although it doesn't seem to me it works correctly, because, for example, computer randomly generates "0.5721765990230501" and this is less than 0.67, right? And according to my if/ else if/ else it should return "paper", not "scissors" but it returns the latter though. Where is the problem?
BTW a little off-topic maybe, but am I correct when I say "returns" in this context [describing my problem] when I mean text printed in console (it's that box in the right, where we see our code processed, isn't it?)? Enlish is not my first language and I would like to use my terms correctly. Thank you.
This is my code:


var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
console.log(computerChoice);

if (0 < computerChoice <= 0.33 ) {
    computerChoice = "rock";
}

else if (0.33 < computerChoice <= 0.66) {
    computerChoice = "paper";    
}

else {
    computerChoice ="scissors";
};


#2

JavaScript does not support chained comparisons. So this code:

0 < computerChoice <= 0.33

should be turned into:

0 < computerChoice && computerChoice <= 0.33

But you know that Math.random() does not return the negative values so you can simply write:

computerChoice <= 0.33

In the else if you know that condition of if was false so you don't have to check if computerChoice < 0.33, you know this is true, so you can use:

computerChoice <= 0.66

#3

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