Back to all solutions

#2620 - Counter

Problem Description

Given an integer n, return a counter function. This counter function initially returns n and then returns 1 more than the previous value every subsequent time it is called (n, n + 1, n + 2, etc).

Solution

/**
 * @param {number} n
 * @return {Function} counter
 */
var createCounter = function(n) {
  let count = n;
  return () => count++;
};