function makeStepCounter(step) {
let n = 0
return () => (n += step)
}
const byFive = makeStepCounter(5)
byFive() // β 5
byFive() // β 10
byFive() // β 15Capturing a custom step in the closure means each produced counter advances by its own preset increment, independent of all others.
Create a counter that increments by a custom step value instead of always by 1.
Challenge: Create a makeStepCounter(step) function that returns a function. Each call increments by step and returns the new value.
Example:
const counter = makeStepCounter(5)
counter() // 5
counter() // 10
counter() // 15