Created
March 10, 2015 18:32
-
-
Save pseudosavant/095acf1a5d12a833d5de to your computer and use it in GitHub Desktop.
Bounding function to ensure a value doesn't exceed a given range
This file contains hidden or 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
// Bounding function to ensure a value doesn't exceed a given range | |
function bound(bottom, top, v) { | |
return Math.max(bottom, Math.min(top, v)); | |
} | |
// Examples: | |
bound(0,1,2); // 1 | |
bound(0,3,2); // 2 | |
bound(0,1,-2); // 0 | |
bound(-3,1,-2); // -2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment