Create a Counter Function
createCounter(n), which returns a counter function. Its first call returns n, and each later call returns one more than the call before. The count lives in a closure, so two counters never share it, and starting values can be negative.Arguments
n: number —The starting integer for the counter.
Returns
Function — Solution idea
This is a classic closure exercise: the state lives in the outer scope, and only the returned functions can mutate or read it.
// Example 1
const counter = createCounter(10);
counter(); // => 10
counter(); // => 11
counter(); // => 12
// Example 2
const counter = createCounter(-2);
counter(); // => -2
counter(); // => -1
counter(); // => 0Public interview reports
These public, self-reported interview accounts informed this practice version. The exercise is modelled on reported rounds, not copied from them, and the reports are not verified official questions or endorsements.
- Medium: ServiceNow software engineer (UI) interview experience, 2025
One UI engineer account describes writing functions that increment and decrement a variable inside loops to show closure and scope handling. This counter is a simpler relative of that exercise, not the reported wording.
Reported in a public ServiceNow interview account. ServiceNow frontend interview questions
Solution
Overview
This is a classic closure exercise: the state lives in the outer scope, and only the returned functions can mutate or read it.
Approach: Closure-based counter (recommended)
Optionally allow a reset method if the prompt mentions it. Emphasize that the counter is deterministic: the same sequence of calls always produces the same values. Complexity is O(1) per call.
export default function createCounter(n) {
return function() {
const current = n;
n += 1;
return current;
};
}
export default function createCounter(n: number): () => number {
return function(): number {
const current = n;
n += 1;
return current;
};
}
Notes & Pitfalls
Using a global variable will share state across all counters — use a closure per instance. Returning ++nvs. current value changes behavior (off-by-one). This task expects returning the current and then incrementing.Mutating objects captured in closures can cause shared state; here we keep only a primitive number.
Negative starts (e.g., -2) should count upward correctly.Large numbers work until you hit Number.MAX_SAFE_INTEGER(then precision issues can appear).Multiple counters created from separate calls must not interfere with each other.
Leverage lexical scoping: inner function closes over n.Prefer returning a function over exposing mutable objects to keep encapsulation tight.
Common mistakes on this challenge
Counter state is not preserved correctly across calls
This question is about closure state and per-instance isolation. Capture nin an outer scope and return an inner function.Return current value before incrementing for each call. Ensure each createCountercall gets its own independentn.
Resources
Similar questions
Guides
Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.
Built in public
The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.