Goal: create a function
makeCounter(initialValue = 0)that returns a counter function. The first call returns the initial value (or0), each subsequent call returns a value 1 greater. Each counter is independent.
++ operator increments the value by 1function makeCounter(initialValue = 0)count++ (returns the current value, then increments) and ++count (increments, then returns)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:
count variable between calls. This provides independent state for each new counter.count++ is used instead of ++count so that the first call returns the initial value without incrementing.initialValue = 0 simplifies the code.makeCounter(...) creates a new lexical environment, so counter1 and counter2 do not interfere with each other.Create a function makeCounter that takes an optional integer as the initial value and returns a counter function.
makeCounter create separate countersconst counter = makeCounter();
counter(); // 0
counter(); // 1
counter(); // 2const counter = makeCounter(5);
counter(); // 5
counter(); // 6
counter(); // 7const counter1 = makeCounter(10);
const counter2 = makeCounter(20);
counter1(); // 10
counter2(); // 20
counter1(); // 11
counter2(); // 21makeCounterGoal: create a function
makeCounter(initialValue = 0)that returns a counter function. The first call returns the initial value (or0), each subsequent call returns a value 1 greater. Each counter is independent.
++ operator increments the value by 1function makeCounter(initialValue = 0)count++ (returns the current value, then increments) and ++count (increments, then returns)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:
count variable between calls. This provides independent state for each new counter.count++ is used instead of ++count so that the first call returns the initial value without incrementing.initialValue = 0 simplifies the code.makeCounter(...) creates a new lexical environment, so counter1 and counter2 do not interfere with each other.Create a function makeCounter that takes an optional integer as the initial value and returns a counter function.
makeCounter create separate countersconst counter = makeCounter();
counter(); // 0
counter(); // 1
counter(); // 2const counter = makeCounter(5);
counter(); // 5
counter(); // 6
counter(); // 7const counter1 = makeCounter(10);
const counter2 = makeCounter(20);
counter1(); // 10
counter2(); // 20
counter1(); // 11
counter2(); // 21makeCounterThe 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!