QUICK START:HTMLCSSJavaScriptTypeScriptPythonSQLReactNext.jsNode.jsDSASystem DesignDevOpsCybersecurityAI / ML
Output PredictionBeginner

Stateful Counter with Closures

Create a function `createCounter(init)` that returns an object with `increment()`, `decrement()`, and `reset()` methods maintaining internal encapsulated state.

Requirements:

  • increment() increases value by 1 and returns it.
  • decrement() decreases value by 1 and returns it.
  • reset() resets value back to initial value and returns it.
  • Do not expose internal variable directly.
Sample Input:
const counter = createCounter(5); counter.increment(); counter.reset(); counter.decrement();
Sample Output:
6, 5, 4
javascript• Solution Editor
EXECUTION CONSOLE
Click “Run & Test Code” to execute your solution against test cases.