Boolean operator in random number generation

Why does this code seem to work…
var number = randomNumber(0, 10)||randomNumber(15, 20);

A student used this to pick a random number between 0 and 10, or between 15 and 20. In my mind, this should not work, but it seems to. Does this truly work as intended, or is something else going on here?

greetings @bvangorden,

Explanation

the simplest explanation is that it’s an inline boolean expression being evaluated.
unlike lower level languages JS types can be converted based on the expression being used in the program for instance the number 0 can be resolved to false as a boolean without having to convert it, this can lead to some unintended side effects

in your snippet the student uses || an or operator. as you know this looks for an expression that evaluates to true, (if no values of the operand are true will resolve to the last operand value of the or statement). values such as null, 0, false, "" can all be perceived to evaluate as fasle. with that knowledge let’s step over what’s happening

Stepthrough

→ var number is being set
→ randomNumber is being executed between 0 - 10
→ || or operator is checking for a true statement to resolve the variable to be set to if false will goto next operand
→ if first statement is calculated as false run other operand on the or statement
→ second randomNumber being executed between 15 - 20

Answer

did you see it yet? if not it’s 0 being interpreted as false in the || comparison statement leading to it being an incomplete comparison method. below i have provided a more effective solution that may be a better solution to show

Example Solution

function customRange(min1, max1, min2, max2) {
    var number = randomNumber(min1, max2);
    if (number > max1 && number < min2) {
        number = Math.abs(number - max1) < Math.abs(max2 - max1) / 4 ? randomNumber(min1, max1) : randomNumber(min2, max2);
    }
    return number;
}
console.log(customRange(0, 10, 15, 20))

here we execute the total range and if it’s inbetween the two ranges we check to see what it’s closest to. however this solution only works if the max ranges difference is > 1 otherwise it will always evaluate to the lower range if it’s inbetween the two of them. you could also implement a check to ensure both ranges don’t interfere with it but it would be my solution if this was the only test case

Conclusion

if your students don’t have any idea how this works don’t let them use it. with all code it is important you as the programmer understand how it works. not to mention that the range of numbers isn’t complete since 0 won’t be included in the range which means you will get 1-10 and 15-20 NOT 0-10 and 15-20

hope this helps

Varrience

1 Like

So, in short, it seems that OR will evaluate 0 as false and any nonzero number as true.

Correct i believe this can be considered a version of type coercion Type coercion - MDN Web Docs Glossary: Definitions of Web-related terms | MDN here’s something on it if you wish to learn more about it since there are plenty of other types that can be effected by certain evaluation

Thank you for your help! I was aware of type coercion for numbers/strings, but was unaware of the boolean/number issue.