Default 'if- else if' solution is false


#1


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

If computerChoice is between 0 and 0.33, make computerChoice equal to "rock".

If computerChoice is between 0.34 and 0.66, make computerChoice equal to "paper".

If computerChoice is between 0.67 and 1, make computerChoice equal to "scissors".

This is the default accepted solution:


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

But I think this is the right solution:

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


#2

then the "correct" solution would have to be:

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

comparison have to be done at both side of the and operator, but the other solution is also fine.

I mean, the random number (you generate) is between 0 and 1, so if check for less then .34 (rock) and less .67 (paper) you covered it everything, why would it be false? You have control over the random number generated, if it was user input, you would have to use && to also check the minimum (greater then 0) and the maximum (smaller then 1)


#3

Yes, it goes from first condition to the latest, and breaks when it is right, so the example is not bad like I thought.


#4

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