JS Built-ins
Closuresbeginner

Closures β€” Add factory

πŸ“– Usage Example
function makeAdder(n) {
  return x => x + n
}
const add5 = makeAdder(5)
add5(3)   // β†’ 8
add5(10)  // β†’ 15

Factory functions use closures to capture a preset value, producing specialized functions without repeating configuration.

Function Factory for Addition

Create a factory function that produces functions with preset behavior.

Challenge: Create a makeAdder(n) function that returns a function. The returned function takes a value and returns the sum of the preset value and the argument.

Example:

const add5 = makeAdder(5)
add5(3)   // 8
add5(10)  // 15
πŸ’‘ Hints (2)
  • β€’ Return a function that adds the captured value to its argument
  • β€’ The factory captures the preset value in closure