HardJavaScriptPractice mode: untimed, 3 hints
Cart total with async prices
cartTotal(items) looks up each product's price with the async fetchPrice(sku) and returns (a promise of) the cart total in rupees. items: [{ sku, qty }].
The code has exactly one planted bug. Reproduce it with the tests, fix it with the smallest change, then submit.
Visible tests
two items
- cartTotal([{"sku":"apple","qty":2},{"sku":"milk","qty":1}]) → 88
empty cart
- cartTotal([]) → 0
Hints 0/3
const PRICES = { apple: 30, bread: 45, milk: 28, eggs: 72 };
// Pretend network call: resolves with the price after a short delay.
async function fetchPrice(sku) {
await new Promise((resolve) => setTimeout(resolve, 1));
if (!(sku in PRICES)) throw new Error(`Unknown product: ${sku}`);
return PRICES[sku];
}
// Looks up each product's price and returns the cart total in rupees.
async function cartTotal(items) {
let total = 0;
items.forEach(async (item) => {
const price = await fetchPrice(item.sku);
total += price * item.qty;
});
return total;
}
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.