24 Game
React.js
hard
90 mins
Requirements
Functional Requirements
- The game should deal 4 random numbers (from 1 to 9) at the start of every new round.
- Players must use all 4 numbers exactly once to create an arithmetic expression that evaluates to 24.
- Players can build expressions by clicking:
- Number cards
- Operators:
+,-,*,/ - Parentheses:
(and)
- The current expression should be displayed as the player builds it.
- A live preview of the evaluated result should be shown whenever the expression is valid.
- A Check button should validate whether the expression equals 24 and provide:
✅for correct solutions❌for incorrect solutions
- A Clear button should reset the current expression without changing the dealt cards.
- An Undo button should remove the most recently entered character or number.
- A New Deal button should generate a completely new set of 4 random numbers.
- A Shuffle button should randomly reorder the currently displayed cards.
- A Give Up button should verify whether the current set of cards is solvable:
- If solvable, the user should be encouraged to keep trying.
- If unsolvable, a new deal should be generated automatically.
- A move history should record all attempts, for example:
3+8+8+5 = 24 ✅(6/2)*(9-1) = 24 ✅
- Optionally, a Hint button may provide a simple suggestion, such as pairing large and small numbers.
Edge Cases and Constraints
- All four numbers must be used exactly once; using fewer than four numbers is invalid.
- A card cannot be selected more than once in a single expression.
- Once a card is used, its button should be disabled.
- Invalid mathematical expressions should display:
Invalid Expression ❌
- The application should not crash when evaluating malformed expressions.
- Division by zero must be handled safely and ignored by the solver logic.
- Starting a new deal should:
- Clear the current expression
- Reset the result display
- Re-enable all cards
- Undo should correctly re-enable a card if the removed character was a number.
- Move history may either:
- Persist across deals, or
- Be cleared when a new game starts
- The layout should be responsive and work well on both desktop and mobile devices.
Testing Identifiers (data-testid)
Cards
card-0card-1card-2card-3
Buttons
btn-addbtn-subbtn-mulbtn-divbtn-lpbtn-rpbtn-undobtn-clearbtn-checkbtn-newbtn-shufflebtn-giveup
Displays
expression-displayresult
Containers
cards-containerhistory-container
Preview what you need to build
Companies:
startups
Solve Similar questions 🔥
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
