Skip to content

Instantly share code, notes, and snippets.

@Woodsphreaker
Last active June 20, 2017 15:52
Show Gist options
  • Save Woodsphreaker/d4e993626b5aa4a35f06c1ec25e9a694 to your computer and use it in GitHub Desktop.
Save Woodsphreaker/d4e993626b5aa4a35f06c1ec25e9a694 to your computer and use it in GitHub Desktop.
clamp.js
const testCondition = (min, max, x) => {
if (x < min) return min
if (x > max) return max
return x
}
const clamp = (min, max, x) =>
testCondition(min, max, x)
// Challenged by lubien ;-)
const clampReduce = (...args) =>
args.reduce((acc, cur) =>
testCondition(cur[0], cur[1], cur[2]), 0)
console.log(clamp(0, 100, 1))
console.log(clamp(0, 100, 7))
console.log(clamp(0, 100, 9))
console.log(clamp(0, 100, 200))
console.log(clamp(0, 100, -100))
console.log(clampReduce([0, 100, 1]))
console.log(clampReduce([0, 100, 7]))
console.log(clampReduce([0, 100, 9]))
console.log(clampReduce([0, 100, 200]))
console.log(clampReduce([0, 100, -100]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment