Closures and lexical scope

A closure is a function that remembers the variables from the scope where it was defined — even after that outer function has returned.

Write a function makeCounter that:

  • Takes a starting integer start
  • Returns a new function that, each time it's called, returns the next integer (start, start+1, start+2, …)
const count = makeCounter(5);
count()  →  5
count()  →  6
count()  →  7

Your program receives:

  1. A starting integer
  2. How many times to call the counter

Print each result on its own line.


Constraints

  • makeCounter must use a closure — no global variables.
Ad SpaceGoogle AdSense active for guests & free-tier learners.