Use a numerical comparison. The following number extension can check if a number of two values ββfalls:
Number.prototype.between = function(lower,upper, includeBoundaries){ lower = Number(lower); upper = Number(upper); noCando = isNaN(lower) || isNaN(upper) || lower>=upper; if ( noCando ) { throw 'wrong arguments or out of range'; } return includeBoundaries ? this >= lower && this <= upper : this > lower && this < upper }; // usage: (12).between(1,12); /=> false (12).between(1,12,true); /=> true (12).between(0,15,true); /=> true (0).between(-5,1); //=> true
The function converts the parameters to a number, because 0 can be calculated in boolean in javascript in order to be able to check whether the variables are real numerical values ββand to check if the lower value is at most / equal to the upper one. In these cases, an error occurs.
The includeBoundaries parameter also checks if the number is equal to lower or upper, if it is not specified, the function returns a real check between.
Kooiinc
source share