Skip to content

Instantly share code, notes, and snippets.

@marshall007
Created April 19, 2012 20:45
Show Gist options
  • Select an option

  • Save marshall007/2424018 to your computer and use it in GitHub Desktop.

Select an option

Save marshall007/2424018 to your computer and use it in GitHub Desktop.
Math.limitRange function
(function() {
Math.limitRange = function(value, min, max) {
return Math.max(Math.min(value, max), min);
};
// test cases
var tests = [[50, 0, 100], // 50
[-25, 5, 15], // 5
[7, 5, 15], // 7
[19, 36, 38]]; // 36
for (var i = 0; i < tests.length; i++) {
console.log(Math.limitRange(tests[i][0], tests[i][1], tests[i][2]));
}
})();​
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment