6. Cart Summary with Coupon, Shipping & VAT
An Egyptian electronics store shows an order summary on the cart page: subtotal, coupon discount, shipping, 14% VAT and total. Finance found that totals on the site sometimes differed from the invoices by a piaster, because of floating-point maths and inconsistent rounding. Marketing also complained that a coupon could push an order below the free-shipping threshold without the shopper noticing.
Rebuild the calculation as three small functions, so each rule can be tested on its own and then together. All amounts are integers in piasters.
Your task:
- calculateDiscount(subtotal, coupon): the coupon discount for a subtotal.
- calculateShipping(afterDiscount): the shipping fee.
- calculateCartSummary(items, coupon): returns { subtotal, discount, shipping, vat, total, couponApplied }, using the two functions above.
Rules:
- items is a list of { sku, unitPrice, quantity }. Lines with quantity <= 0 are ignored. subtotal = sum of unitPrice x quantity.
- coupon is null or { code, type, value, minSubtotal }, with type "percent" or "fixed". It applies only when subtotal >= minSubtotal; otherwise the discount is 0.
- percent: discount = round(subtotal x value / 100). fixed: discount = min(value, subtotal).
- Shipping is 0 when afterDiscount >= 7500, otherwise 799. The threshold uses the amount after the discount.
- vat = round(afterDiscount x 14 / 100). Shipping is not taxed.
- total = afterDiscount + shipping + vat.
- couponApplied is true when a coupon was given and met its minimum.
- An empty cart (no counted lines) returns all zeros and couponApplied false.
- "round" means round half up to the nearest piaster. Multiply before you divide so you stay in integers.
What you'll write
calculateDiscountfunctionDiscount in piasters for a subtotal and a coupon (or null).calculateShippingfunctionShipping fee for the amount after the discount.calculateCartSummaryfunctionThe full order summary. Integration of the two functions above.
Each one has its own test cases, and the last one is also tested together with the others. Implement them in order.
Examples
Example 1
- Function
calculateCartSummary- Input
items = [ { sku: "cable", unitPrice: 2999, quantity: 2 }, { sku: "case", unitPrice: 1550, quantity: 1 } ]coupon = { code: "SAVE10", type: "percent", value: 10, minSubtotal: 5000 }
- Output
{ subtotal: 7548, discount: 755, shipping: 799, vat: 951, total: 8543, couponApplied: true }
- Explanation
- 10% of 7548 is 754.8, rounded to 755. 6793 after the discount is under 7500, so shipping is charged. VAT is 14% of 6793 = 951.02, rounded to 951.
Example 2
- Function
calculateDiscount- Input
subtotal = 4999
coupon = { code: "SAVE10", type: "percent", value: 10, minSubtotal: 5000 }
- Output
0
- Explanation
- The subtotal is below the coupon minimum.
Solution
Write calculateDiscount, calculateShipping and calculateCartSummary. Tab indents (Esc then Tab leaves the editor), Ctrl/⌘ + Enter runs. Each run has a 3-second limit. Your code is saved in this browser.
Test Results
Click "Run Code" to test your solution.