JS Built-ins
Closuresintermediate

Closures β€” Range validator factory

πŸ“– Usage Example
function makeValidator(min, max) {
  return value => value >= min && value <= max
}
const isAdult = makeValidator(18, 120)
isAdult(25)  // β†’ true
isAdult(15)  // β†’ false

Validator factories close over min and max bounds, allowing reusable range-checking functions to be created with any boundary values.

Range Validation Factory

Create a factory that produces validator functions for checking if values fall within a range.

Challenge: Create a makeValidator(min, max) function that returns a function. The returned function takes a value and returns true if the value is between min and max (inclusive), otherwise false.

Example:

const isInRange = makeValidator(10, 20)
isInRange(15)  // true
isInRange(5)   // false
isInRange(25)  // false
πŸ’‘ Hints (2)
  • β€’ Use comparison operators to check if value is in range
  • β€’ Boundary values should be included in the range