FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Medium

Use Countdown

25mins

Implement a hook useCountdown(initialValue, interval) that manages a countdown timer.

The hook should:

  • Accept an initial number value and an interval in milliseconds.
  • Return an object containing:
    • value: current countdown value.
    • start(): function to start the countdown.
    • pause(): function to pause the countdown.
    • reset(): function to reset to the initial value.
  • Automatically decrement the value by 1 at each interval until it reaches 0.

Examples

const countdown = useCountdown(10, 1000); countdown.start(); // countdown.value decreases from 10 to 0 every second countdown.pause(); // pauses the countdown countdown.reset(); // resets value to 10

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests