🟨 JavaScript
Easy
🕐 5 min

Counter Function: makeCounter

Goal: create a function makeCounter(initialValue = 0) that returns a counter function. The first call returns the initial value (or 0), each subsequent call returns a value 1 greater. Each counter is independent.

💡 Solution hint:
  • Use closures to preserve the counter state
  • The ++ operator increments the value by 1
  • You can use a default parameter value: function makeCounter(initialValue = 0)
  • Remember the difference between count++ (returns the current value, then increments) and ++count (increments, then returns)
👀 Peek at solution:
function makeCounter(initialValue = 0) {
    // Private state variable — accessible only from the returned function
    let count = initialValue;
    
    // Return the counter function: returns current value, then increments
    return function() {
      return count++; // post-increment: return old value first, then increment
    };
}
  
window.makeCounter = makeCounter;

Why this way:

  1. A closure is used to keep the count variable between calls. This provides independent state for each new counter.
  2. count++ is used instead of ++count so that the first call returns the initial value without incrementing.
  3. Default parameter initialValue = 0 simplifies the code.
  4. Each call to makeCounter(...) creates a new lexical environment, so counter1 and counter2 do not interfere with each other.

Task description

Create a function makeCounter that takes an optional integer as the initial value and returns a counter function.

How the function should work:

  1. First call of the returned function should return the initial value (if provided) or 0 (if not)
  2. Each subsequent call should return a value 1 greater than the previous call
  3. Each counter is independent — different calls to makeCounter create separate counters

Usage examples:

Counter without initial value:

const counter = makeCounter();
counter(); // 0
counter(); // 1
counter(); // 2

Counter with initial value:

const counter = makeCounter(5);
counter(); // 5
counter(); // 6
counter(); // 7

Independent counters:

const counter1 = makeCounter(10);
const counter2 = makeCounter(20);
 
counter1(); // 10
counter2(); // 20
counter1(); // 11
counter2(); // 21

Requirements:

  • The function must be named makeCounter
  • Accepts one optional parameter — initial value
  • Returns a function that increments the counter by 1 on each call
  • If the initial value is not provided, the counter starts from 0
  • Different counters must work independently

🧑‍💻 It's not a bug! It's a feature!

The code editor is intentionally hidden on mobile.

Believe me, it's for the best: I am protecting you from the temptation to code in less-than-ideal conditions. A small screen and a virtual keyboard are not the best tools for a programmer.

📖 Now: Study the task, think through the solution. Act like a strategist.

💻 Later: Sit down at your computer, open the site, and implement all your ideas comfortably. Act like a code-jedi!