Why No Difference?


#1

For the rocks-paper-scissors exercise, I had this code:

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

But CodeAcademy changed it to:

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

So I'm now wondering why there's no difference, since to my mind, my way of thinking, saying if (computerChoice < 0.34) and then else if(computerChoice <= 0.67) should be problematic since the latter case covers the former, too -- that is, both computerChoice < 0.34 and computerChoice <=0.67 should be the same...and thus this code is different than mine!!

Anyone know what I mean?

My code explicitly provides for a range -- it tries to translate the word "between" into numbers -- whereas Code Academy's substitution seems to subsume one case (less than 0.34) within another (less than or equal to 0.67)...and yet both my and Code Academy's code appear to function the same!!!

But they shouldn't! Logically, they're not the same: I'm saying "between these values" (as per the original instructions) and Code Academy's sort of saying "less than this, less than that"....

Thanks for all advice.


#2

Hi dc_super_rich,

CodeAcademy's code works because there is an "ELSE if"

So it goes to to the 'else if computerChoice < 0.67' condition only if computerChoice is greater than 0.34 to start with.


#3

First of all random number exercises are hard to test so I guess anything that roughly divides it into 3 part of almost equal size counts. Whether its 0.34, 0.333333, 1/3 or what ever doesn't really matter neither does a "less than" or "less than or equal to" does.

Now as @netrunner86697 already tried to explain an else if is really just a shorter way of writing

if(condition){
    ...
}
else{
    if(condition2){
      ...
    }
}

The only difference is that you get rid of the nesting. So in other words you only go on with the else case if the if case was already wrong. Next thing that comes into play is the range of Math.random. As it is limited from 0 to 1 you already are sure that it is >= 0 so asking this is redundant. Redundant doesn't mean that it is wrong and you can interprete the exercise like this (and its not an uncommon way to interprete it) but it is not necessary because of the boundary condition. Same would hold for < 1 but you already got rid of this by using else.
So the first if can be shorten and for the second well you already made sure that the value is > 0.33 because it was no <= 0.33 in the first case so asking for this is again redundant, isn't it?


#4

Hey thanks everyone for helping me think through this!

I think my problem is that I'm not a terribly numerate kinda guy -- in fact, if numbers were words, I'd be very close to functionally illiterate! -- so yeah, I took Code Academy's instructions "literally" instead of thinking about the "bigger picture" of what those instructions are trying to achieve....

Thus I tried to "translate" their word "between" into numbers word-for-number (one-to-one) which matches the English quite closely (or so it seems to me) but numerically/logically is redundant and silly....

Anyway, thanks again!!!