The clampNumber function returns the nearest number within the specified range if num does not fall within that range. Specifically, it returns a if num is less than a, and it returns b if num is greater than b.
Here’s the function for reference:
const clampNumber = (num, a, b) =>
Math.max(Math.min(num, Math.max(a, b)), Math.min(a, b));
For example:
clampNumber(2, 3, 5);returns3(since 2 is less than 3).clampNumber(6, 3, 5);returns5(since 6 is greater than 5).
