Skip to content
intermediate

Review: a countdown stuck one second in

This is recall. The code looks finished and is wrong. Find the defect and fix it. You are graded on tests you cannot see.

You asked a model for a countdown hook: give it a number of seconds, get back the seconds remaining, ticking down once per second and stopping at zero. It produced this, with a test. The test passes.

import { useEffect, useState } from 'react'

/**
 * Counts down from `start` once per second, stopping at zero.
 */
export function useCountdown(start: number): number {
  const [seconds, setSeconds] = useState(start)

  useEffect(() => {
    const id = setInterval(() => {
      if (seconds > 0) {
        setSeconds(seconds - 1)
      }
    }, 1000)
    return () => clearInterval(id)
  }, [])

  return seconds
}
review — find the defectCtrl+↵ submits · Ctrl+Shift+↵ runsstarter code