All Challenges

Shopping Cart & Promo Calculator

ReactMediumFlipkartNykaaZepto
45:00

Problem Brief

Build an interactive checkout cart with +/- quantity adjustments, item removal, SAVE20 (20% off) promo validation, and live subtotal/total calculation.

Requirements

  • Keep products (`Hoodie` Rs. 1000, `Sneakers` Rs. 2000) editable via `.qty-inc`, `.qty-dec`, and `.remove-item`.
  • Decreasing quantity from 1 removes or clamps cleanly; update `[data-testid="subtotal"]`.
  • Applying code `SAVE20` via `[data-testid="promo-input"]` + `[data-testid="apply-promo"]` applies a 20% discount.
  • Invalid promo codes show an error message inside `[data-testid="promo-msg"]` and apply 0 discount; update `[data-testid="final-total"]`.
Progressive Hints (1)
  • Compute `subtotal`, `discount = promoApplied ? Math.round(subtotal * 0.2) : 0`, and `finalTotal = subtotal - discount` inside one derived `render()`.
Reference Solution

Stuck? Load the verified reference code into your editor.

Automated Test Suite

5 Test Cases Ready
  • Renders initial cart subtotal and total (3000)Shows 2 items totaling 3000 initially
  • Incrementing quantity updates subtotal cleanlyClicking .qty-inc on Hoodie adds 1000 to subtotal (4000)
  • Applies valid SAVE20 promo for 20% discountSAVE20 reduces 3000 total to 2400
  • Rejects invalid promo codes with feedback messageFake code shows error in promo-msg and applies 0% discount
  • Removing or decrementing item to 0 deletes line itemRemoves line item and updates totals to 2000
solution.html
Live Sandboxed Previewiframe sandbox
Starter template loaded — add your <script> logic in the editor (or open Reference Solution) to make the widget interactive.