Coding Challenge - #31: Clamp Number Range

Write a function clamp(value, min, max) that restricts a given number to fall within an inclusive minimum and maximum range. For example, clamp(12, 0, 10) should return 10, clamp(-4, 0, 10) should return 0, and clamp(5, 0, 10) should return 5.

function clamp(value, min, max) {
  // your code here
}

Rules:

  • Must return the value itself if it falls within min and max
  • Must return min if value is less than min
  • Must return max if value is greater than max
  • Assume min is always less than or equal to max

Post your solution as a reply. Answer goes up in about a day.