Skip to content

Instantly share code, notes, and snippets.

@pseudosavant
Created March 10, 2015 18:32
Show Gist options
  • Save pseudosavant/095acf1a5d12a833d5de to your computer and use it in GitHub Desktop.
Save pseudosavant/095acf1a5d12a833d5de to your computer and use it in GitHub Desktop.
Bounding function to ensure a value doesn't exceed a given range
// 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