Files
30-seconds-of-code/snippets/clampNumber.md
Selim Ajimi 4b0bc342c1 Fix typos in spaces
Fix typo to fit the context

Fix typo add space

Fix small typo

Fix small typo

Fix typo

Fix typo

Fix typo

Fix typo

Fix typo

Fix typo

Fix typo

Update tail.md

Fix typo

Fix small typo

Fix typo

Fix typo

Fix typo

Fix typo

Fix small typo

Fix typo

Fix small typo

Fix typo

Fix typo

Fix typo

Fix typo

Fix typo

Fix typo

Fix typo

Fix typo

Fix typo
2017-12-22 23:48:42 +01:00

507 B

clampNumber

Clamps num within the inclusive lower and upper bounds.

If lower is greater than upper, swap them. If num falls within the range, return num. Otherwise, return the nearest number in the range.

const clampNumber = (num, lower, upper) => {
  if(lower > upper) upper = [lower, lower = upper][0];
  return (num>=lower && num<=upper) ? num : ((num < lower) ? lower : upper) 
}
// clampNumber(2, 3, 5) -> 3
// clampNumber(1, -1, -5) -> -1
// clampNumber(3, 2, 4) -> 3