Read here. Code on desktop. The coding workspace opens on screens 1024px and wider.
JavaScript interview question
Clamp
easy10 min
Implement a utility that keeps a number inside an inclusive lower and upper bound.
Requirements
Implement clamp(value, lower, upper). Return lower when value is below lower, upper when above upper, or value when already inside the range.
Both bounds are inclusive. Inputs are numbers with lower ≤ upper; integers, negative numbers, fractions, and infinities are allowed. NaN and invalid ranges are out of scope.
Do not mutate state or use an external library.
Examples
Example 1
Input: clamp(12, 0, 10)
Output: 10
Example 2
Input: clamp(4, 0, 10)
Output: 4
Implement a utility that keeps a number inside an inclusive lower and upper bound.