Design a counter object that supports incrementing, decrementing, and resetting its internal value.
Implement a function createCounter(n) that returns an object with three methods:
increment() increases the current value by 1 and returns the new value.decrement() decreases the current value by 1 and returns the new value.reset() restores the current value back to the original value n and returns it.The returned object should keep its own independent state for each counter created.
Use function scope or closures to preserve private state across method calls.
n used to initialize the counter.increment()decrement()reset()reset() must restore the counter to the initial value passed to createCounter.Example 1
Input
createCounter(5) call increment() call increment() call decrement() call reset() call decrement()
Output
6 7 6 5 4
Explanation
Starting from 5: increment -> 6, increment -> 7, decrement -> 6, reset -> 5, decrement -> 4.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.