JS Built-ins
Closuresbeginner

Closures β€” Multiply factory

πŸ“– Usage Example
function makeMultiplier(n) {
  return x => x * n
}
const double = makeMultiplier(2)
const triple = makeMultiplier(3)
double(5)  // β†’ 10
triple(5)  // β†’ 15

Each call to the factory creates an independent closure capturing its own multiplier, so different instances never interfere with each other.

Function Factory for Multiplication

Create a factory function that produces multiplication functions.

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

Example:

const mult4 = makeMultiplier(4)
mult4(7)  // 28
mult4(3)  // 12
πŸ’‘ Hints (2)
  • β€’ Return a function that multiplies the captured value by its argument
  • β€’ Use the * operator for multiplication