Question presented to candidate: "Build the typewriter effect you see on landing pages: 'I build with React', where the last word types itself out letter by letter, pauses, deletes itself, then types the next word, looping forever. Make the speeds configurable, show a blinking caret, and make sure it is accessible."
Requirements:
- A fixed prefix followed by an animated word from a list (
React,Vue,Angular), looping. - Configurable timings: type speed per character, delete speed per character, hold time on the full word, gap before the next word.
- A blinking caret after the text.
- A Pause button (moving content that runs forever must be pausable).
- Screen readers get the full sentence once, not a stream of partial words.
- Exactly one timer at a time; nothing left running after unmount.
What a strong answer should cover:
- The effect as a state machine:
{ index, count, deleting }with four phases (typing, holding, deleting, gap) and a pureplan(state)that returns the next state and the delay. - A single
setTimeoutper step, scheduled from the current state and cleared before every re-run, instead of nested timers or asetIntervalwith phase flags. - Why a missing cleanup breaks under React StrictMode (two loops, doubled speed) and how to prove it.
- Props or inputs for timings, and keeping the word list referentially stable.
- Accessibility: visually hidden full text, the animated part
aria-hidden, a pause control (WCAG 2.2.2 Pause, Stop, Hide), caret animation off for reduced motion.
Clarifying questions expected:
- "Should it loop forever or stop after the last word?"
- "Should the cursor keep blinking while paused?"
- "Can the word list change at runtime?"
Code / implementation expected: Yes. A reusable component with configurable timings, a single-timer state machine, a caret, a pause control and cleanup.