Skip to content
Tutorials Center
My learning

Topics

Lesson 112 of 132 ยท Projects

Project: Typing Speed Tester

Build a typing speed tester in React. Compute words per minute and accuracy with pure functions, colour each character as you type, and add a timed mode with an interval.

A typing tester shows a passage, listens to what you type, colours each character right or wrong, and reports your speed when you finish. It is a lovely project because it combines three ideas: controlled inputs, a timer that starts on the first keystroke, and results that are all derived from two plain values: the target text and what was typed.

We build it in three stages: the maths as pure functions, the interactive test, and a timed mode.

The plan

Two conventions are standard in typing tests. A "word" is five characters (so speed does not depend on how long your words are), and only correct characters count towards speed:

  • WPM = (correctChars / 5) / minutes
  • accuracy = correctChars / typedChars * 100

State is tiny: the typed string and timing information. Everything else (which characters are correct, how many, the WPM) is computed during render.

Stage 1: the maths without UI

Write and try the formulas as plain functions first. This also shows the edge cases: zero time and nothing typed must not produce NaN or Infinity.

Stage 1: WPM and accuracy

Typing 19 characters perfectly in 6 seconds is 3.8 words in a tenth of a minute, so 38 WPM. One transposed pair costs two correct characters, which lowers both numbers.

Stage 2: the interactive test

The input is controlled. The first keystroke records the start time in a ref (useRef), because the start time never appears on screen. While the test runs, an interval updates a now state so the live WPM refreshes. When the typed text reaches the target length, we freeze the time by storing the end time. The characters are drawn as <span>s coloured by comparing with the target.

Stage 2: the typing test

Things to notice:

  1. running is computed from a ref and a state value. Reading a ref during render is normally discouraged, but here it is safe because every change to the ref is accompanied by a setTyped call that triggers the re-render.
  2. The target text is aria-hidden because the input's label and the results are what a screen reader needs; for a real product you would announce the text differently (accessibility).
  3. A mistake never blocks typing. Real typing tests differ here, so this is a design decision you can change by rejecting wrong characters in handleChange.

Stage 3: a timed mode

Another popular format: type as much as you can in a fixed time. The deadline is known up front, so we show the seconds left and finish automatically at zero. The words are drawn from a list, and we show the user the text as a sequence they continue through. To keep the example compact, the passage repeats a word list.

Stage 3: 10 second sprint

The timer starts in an effect that depends on started, so it begins on the first keystroke and its cleanup stops it on restart. The remaining time is again computed from performance.now(), as you learned in the stopwatch project, so a slow tab cannot make the sprint last longer.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is a 'word' counted as five characters when computing WPM?

    Counting real words would make a passage of long words look slower. Dividing characters by five is a standard, fair convention.
  2. 2. Where is the start time stored, and why?

    A ref persists between renders without causing one. The visible values (WPM, accuracy) come from state and are derived.
  3. 3. Which values are derived during render rather than stored in state?

    They can be calculated from the target and the typed text plus times, so storing them would create values that could go out of sync.
  4. 4. Why compute the remaining time from performance.now() instead of subtracting 100 on each interval tick?

    Timers can fire late. Measuring from the clock keeps the deadline exact.

Summary

  • Compute speed as (correctChars / 5) / minutes and accuracy as correct / typed; guard against division by zero.
  • Start the clock on the first keystroke and keep the start time in a ref.
  • Colour characters by comparing the typed text with the target during render; no per-character state is needed.
  • Use an interval in an effect for live updates, with cleanup, and measure time from the clock.
  • A timed mode only changes when the test ends: at a deadline instead of at the end of the text.

Next: Project: Expense Tracker