FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Easy

useCounter II

15mins

Implement an optimized counter function that returns an object with methods to manipulate the counter.

The returned methods must be memoized, meaning the same function instance is returned every time.

Arguments:

  • initialValue: number — initial counter value. Defaults to 0 if not provided.

Returns: An object containing:

  • count: current counter value
  • increment(): increases the count by 1
  • decrement(): decreases the count by 1
  • reset(): resets count to initialValue
  • setCount(value): sets count to a specific value

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests