TryBugHunt
javascriptLogic errorMedium

Cart total ignores how many of each item

BugFind the bug and fix the code

What it should do

cartTotal(items) adds up price × quantity for every item. This project has two files: cart.js and lineItem.js. Each item looks like { price: 10, quantity: 2 }. Example: [{ price: 10, quantity: 2 }, { price: 5, quantity: 1 }] → 25.

What happens instead

cartTotal returns 15 instead of 25. cart.js only adds up what lineItem.js gives it, so look there too.

This project has 2 files. The symptom shows up in cart.js, but the bug can be in any of them. Click a file name to open it.

const { lineTotal } = require('./lineItem');

function cartTotal(items) {
    return items.reduce((sum, item) => sum + lineTotal(item), 0);
}
module.exports = { cartTotal };

Test a custom input to verify an edge case (e.g. empty lists, negative numbers, duplicates).

⏱️0:00
Hints
🏆 0 hints = Flawless Solve
Think & Test Cooldown
2:00 remaining

Take a moment to read the bug symptom and trace the code with Run & Check before unlocking Hint 1.

Hint 2
Unlocks after Hint 1
Hint 3
Unlocks after Hint 2

This bug belongs to the logic errors pattern

The wrong operator, an inverted condition, a formula the right way round but backwards.

What you'll practise

A medium JavaScript debugging exercise about logic errors: the wrong operator, an inverted condition, a formula the right way round but backwards.

The checks your fix must pass

Signs of this kind of bug

Spoiler: why this bug happens(try it first; the hints get warmer one at a time)

What went wrong: lineTotal returns item.price only, so 2 items at 10 count as 10.

The fix: in lineItem.js, return item.price * item.quantity;

Remember: the file where you see the wrong answer is not always the file with the bug. Follow the function call.

The pattern behind it: logic errors.

More logic errors to practise

Related errors