Created
August 5, 2009 04:54
-
-
Save dandean/162507 to your computer and use it in GitHub Desktop.
Number.prototype.isBetween implementation...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Number#between(low, high[, inclusive = false]) -> boolean | |
* - low(Number): Number must be greater than `low` value | |
* - high(Number): Number must be less than `high` value | |
* - inclusive(Boolean): [Optional] Include low and high numbers in the check. | |
* Defaults to `false`. | |
* | |
* Checks if the Number is between `low` and `high` values. Returns `true` or `false` | |
**/ | |
Number.prototype.isBetween = function(low, high, inclusive) { | |
if (inclusive) { | |
return this >= low && this <= high; | |
} | |
return this > low && this < high; | |
} | |
var five = 5; | |
console.log(five.isBetween(3, 10)); // true | |
console.log(five.isBetween(9, 10)); // false | |
console.log(five.isBetween(5, 10)); // false | |
console.log(five.isBetween(5, 10, true)); // true | |
console.log(Number.MIN_VALUE.isBetween(0, 0.1)); // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment