MediumJavaScriptPractice mode: untimed, 3 hints
Coupon total
totalWithCoupon(cart, percent) returns the cart total after taking percent% off every item, rounded to 2 decimal places. Callers keep using cart afterwards (to show the original prices), so the function must not change it.
The code has exactly one planted bug. Reproduce it with the tests, fix it with the smallest change, then submit.
Visible tests
cart is left unchanged
- totalWithCoupon([{"name":"pen","price":100,"qty":2}], 10) → 180 · input must not change
no discount
- totalWithCoupon([{"name":"mug","price":250,"qty":1},{"name":"pad","price":40,"qty":3}], 0) → 370
Hints 0/3
// Returns the cart total after taking `percent`% off every item, rounded to 2 decimal places.
// cart: [{ name, price, qty }]. Callers keep using `cart` afterwards (for example to show the
// original prices), so it must not change.
function totalWithCoupon(cart, percent) {
let total = 0;
for (const item of cart) {
item.price = item.price * (1 - percent / 100);
total += item.price * item.qty;
}
return Math.round(total * 100) / 100;
}
Ctrl/⌘+Enter runs, Ctrl/⌘+Shift+Enter submits. In the editor, Ctrl+M switches Tab to moving focus.
Run works without an account. Log in to submit against the hidden tests and track your accuracy per bug type.
Ready for pressure? Try a timed set: six snippets, 20 minutes, no hints.