Last active
June 20, 2017 15:52
-
-
Save Woodsphreaker/d4e993626b5aa4a35f06c1ec25e9a694 to your computer and use it in GitHub Desktop.
clamp.js
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
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