# 7460 MINNumbers Game
# 74
60 MIN
Overview
Build a Countdown-style numbers game — a great way to practice useReducer with multi-field game state — plus a self-scheduling useEffect stopwatch that freezes on success, immutable updates, and truncating a step history when a step is deleted.
Solution preview
Here's the result you'll build by completing this challenge.
Requirements
- Implement the game in
App.tsxandreducer.ts.NumberTile.tsx,OperatorPad.tsx,StepsList.tsx,utils.tsandindex.cssare read-only. - The app starts idle:
start-gamereads "Start",targetreads---,timerreads0s, notileis rendered, neithererrornoroutcomeis present, and the board shows sixempty-tileplaceholders. - Clicking
start-gamestarts a round:start-gamedisappears, theempty-tileplaceholders are replaced by exactly sixtilebuttons rendered from the generated round, andtargetshows the generated integer from 100 to 999. timercounts elapsed seconds up by one each tick, starting from0s.- Each
tileis a<button type="button">withdata-valueandaria-pressed; clicking a tile selects it and clicking it again deselects it. operator-add,operator-subtract,operator-multiplyandoperator-divideshow+,−,×,÷with accessible labels, and the active operator is reflected inaria-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
stepentry such as25 × 3 = 75is 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 nostep, and clears the pending tile and operator; nooutcomeis shown. Theerrordisappears as soon as the next selection starts. - Reaching the target exactly wins the round: the counter stops, tiles and operators are disabled, an
outcomeis shown, andstart-gamereads "Play again"; noerroris shown. - Every
stephas a delete control atdelete-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 entire history and restores the six starting tiles; deleting the last step removes only that step.
- After a finished round,
start-gamestarts a fresh round with six tiles, a0sclock, and no steps, outcome or error.
Notes
- Keep the whole round in
reducer.ts: tiles, target, steps, pending tile, operator, error, elapsed time and status. One reducer avoids impossible states such as a "won" round with a running clock. - Store the six starting tiles. Deleting a step is then just "truncate the steps to a prefix and replay it from the starting tiles" — no inverse arithmetic needed.
- A step's result can be an operand of the next step, which is why deleting a step must also delete every step after it.
- Derive result ids from the step count: because only prefixes survive a deletion, the next id after
ksteps is always free. - Use a self-rescheduling
setTimeoutand clear it on cleanup. Stop scheduling once the round is no longerplaying, or a late tick could move the clock after a win. applyOperationalready enforces the math rules (positive integers, exact division); call it instead of re-implementing validity.- Match tiles by
id, not by value: small numbers can appear twice. - You do not need to implement the number generator or the solver:
utils.tsalready produces solvable rounds. Focus on the React state, effects and rendering. - React docs:
useReducerand effect cleanup.
Tests
- starts idle with a hidden target, a zeroed clock, and no tiles
- starts a round with six tiles and a three-digit target
- counts elapsed time up one second per tick
- selects a tile and deselects it when clicked again
- marks the chosen operator as pressed
- combines two tiles with addition and records a step
- combines two tiles with subtraction and records a step
- combines two tiles with multiplication and records a step
- combines two tiles with exact division and records a step
- rejects a subtraction that would go negative
- rejects a division that is not exact
- clears the error when the next selection starts
- rejects division by zero and other non-positive results
- wins when the target is reached, freezing the clock and input
- shows a delete control on every step
- deleting a middle step removes it and every later step
- deleting the first step clears the history and restores six tiles
- deleting the last step only removes that step
- starts a fresh round with a clean board after playing again