Files
30-seconds-of-code/snippets/clampNumber.md
Xavey Aguarez 5b9330d634 tightened code for clampnumber
clampNumber is now a single line function
2017-12-27 01:55:32 -08:00

393 B

clampNumber

Clamps num within the inclusive range specified by the boundary values a and b

If num falls within the range, return num. Otherwise, return the nearest number in the range.

const clampNumber = (num, a, b) => Math.max(Math.min(num, Math.max(a,b)),Math.min(a,b));

// clampNumber(2, 3, 5) -> 3
// clampNumber(1, -1, -5) -> -1
// clampNumber(3, 2, 4) -> 3