DEV Community

Cover image for New React Challenge: Numbers Game
ReactChallenges
ReactChallenges

Posted on

New React Challenge: Numbers Game

Six tiles, four operators, one target. Instead of scattering a game across a dozen useState calls, this challenge asks you to keep the whole round in a single useReducer โ€” then add a stopwatch that keeps ticking until you win.


๐Ÿงฉ Overview

Build a Countdown-style numbers game. Combine six number tiles with +, โˆ’, ร— and รท to reach a random three-digit target while a stopwatch measures your time. Along the way you'll practice useReducer for multi-field game state, a self-scheduling effect, and truncating a step history when a step is deleted.

๐Ÿ‘‰ https://www.reactchallenges.com/challenges/numbers-game


โœ… Requirements

  • Implement the game in App.tsx and reducer.ts. NumberTile.tsx, OperatorPad.tsx, StepsList.tsx, utils.ts and index.css are read-only.
  • The app starts idle: start-game reads "Start", target reads ---, timer reads 0s, no tile is rendered, neither error nor outcome is present, and the board shows six empty-tile placeholders.
  • Clicking start-game starts a round: it disappears, the placeholders become exactly six tile buttons, and target shows the generated integer from 100 to 999.
  • timer counts elapsed seconds up by one each tick, starting from 0s.
  • Each tile is a <button type="button"> with data-value and aria-pressed; clicking selects it, clicking again deselects it.
  • The four operator buttons show +, โˆ’, ร—, รท with accessible labels, and the active operator is reflected in aria-pressed.
  • With one tile selected and an operator chosen, clicking a second tile combines them: both operands leave the board, the result becomes a new tile, and a step such as 25 ร— 3 = 75 is added.
  • Addition and multiplication always combine; subtraction only when the result is positive, and division only when it is exact with a non-zero divisor.
  • An invalid combination shows an error, keeps every tile, adds no step, and clears the pending tile and operator; the error disappears as soon as the next selection starts.
  • Reaching the target exactly wins the round: the counter stops, tiles and operators are disabled, an outcome is shown, and start-game reads "Play again".
  • Every step has a delete control at delete-step-{index}.
  • Deleting a step removes that step and every step after it, and rebuilds the board to the state it had before that step.
  • Deleting the first step clears the history and restores the six starting tiles; deleting the last step removes only that step.
  • After a finished round, start-game starts a fresh round with six tiles, a 0s clock, and no steps, outcome or error.

๐Ÿ’ก Notes

  • Keep the entire round in reducer.ts: tiles, target, steps, pending tile, operator, error, elapsed time and status. One reducer avoids impossible states like a "won" round with a running clock.
  • Store the six starting tiles. Deleting a step then becomes "truncate the steps to a prefix and replay it from the starting tiles" โ€” no inverse arithmetic needed.
  • Use a self-rescheduling setTimeout and clear it on cleanup, and stop scheduling once the round is no longer playing so a late tick can't move the clock after a win.

๐Ÿงช Tests

  1. starts idle with a hidden target, a zeroed clock, and no tiles
  2. starts a round with six tiles and a three-digit target
  3. counts elapsed time up one second per tick
  4. selects a tile and deselects it when clicked again
  5. marks the chosen operator as pressed
  6. combines two tiles with addition and records a step
  7. combines two tiles with subtraction and records a step
  8. combines two tiles with multiplication and records a step
  9. combines two tiles with exact division and records a step
  10. rejects a subtraction that would go negative
  11. rejects a division that is not exact
  12. clears the error when the next selection starts
  13. rejects division by zero and other non-positive results
  14. wins when the target is reached, freezing the clock and input
  15. shows a delete control on every step
  16. deleting a middle step removes it and every later step
  17. deleting the first step clears the history and restores six tiles
  18. deleting the last step only removes that step
  19. starts a fresh round with a clean board after playing again

By the end you'll have a clean mental model for reducer-driven game state, effects that reschedule themselves, and history you can rewind without writing a single inverse operation.

๐Ÿ”ฅ Start the Challenge Now

Top comments (0)