The Checkout Meltdown — JavaScript Bug Hunt

Shoppers on Lumen Store are seeing wildly wrong totals at checkout the moment they apply a coupon.

  • Language: JavaScript
  • Layer: Frontend
  • Difficulty: Easy
  • Modelled on: Classic e-commerce
  • Visible tests: subtotal multiplies price by quantity; 20% coupon discounts the subtotal once; no coupon means no discount
  • Reward: 50 XP for a complete fix

Briefing

Shoppers on Lumen Store are seeing wildly wrong totals at checkout the moment they apply a coupon. The cart state itself is fine — the bug lives somewhere in the pure calculation layer that the React checkout page calls.

You have three files. Only calculateTotals.js is editable — the formatter and constants are shared with other teams and locked.

Fix the calculation so every test in the suite passes.

Bug report

BUG-2107 · Priority: Critical · Reported by: support (14 duplicate tickets)

Steps to reproduce:

  1. Add "Desk Lamp" ($40.00) ×2 to the cart
  2. Apply coupon SAVE20 (20% off)
  3. Observe the order summary

Expected: subtotal $80.00, discount $16.00, total $69.12 (with 8% tax) Actual: discount shows $64.00 and the total goes NEGATIVE on larger carts.

Also flagged by QA: a 100%-off promo coupon produces a total of exactly $0.00 in staging but a small positive number in prod.

Logs

console.error: [checkout] total sanity check failed { subtotal: 80, discount: 64, total: -1.28 }
console.error: [checkout] total sanity check failed { subtotal: 210, discount: 168, total: -3.36 }

The code as shipped

src/cart/calculateTotals.js (editable)

var constants = require("./constants");

// items: [{ name, priceCents, quantity }]
// coupon: { rate } where rate is 0..1 (0.2 = 20% off) — or null
exports.calculateTotals = function (items, coupon) {
  var subtotalCents = 0;
  for (var i = 0; i < items.length; i++) {
    subtotalCents += items[i].priceCents;
  }

  var rate = coupon ? coupon.rate : 0;
  if (rate > constants.MAX_DISCOUNT_RATE) rate = constants.MAX_DISCOUNT_RATE;

  var discountCents = Math.round(subtotalCents * rate * items.length);
  var taxedCents = Math.round((subtotalCents - discountCents) * (1 + constants.TAX_RATE));

  return {
    subtotal: subtotalCents / 100,
    discount: discountCents / 100,
    total: taxedCents / 100,
  };
};

Read-only context: src/cart/constants.js, src/cart/formatPrice.js.

Open the hunt to edit the files, run the visible tests and submit against the hidden ones. More JavaScript bug hunts.