Spot the array issue in this quirky tea kettle inventory helper.
const kettleInventory = [
{ model: "Copper Whistle", stock: 4 },
{ model: "Glass Steep", stock: 0 },
{ model: "Cast Iron Classic", stock: 7 },
{ model: "Electric Quick-Boil", stock: 2 }
];
function formatKettleListing(item) {
return `${item.model} (${item.stock} in stock)`.trim();
}
function auditStock(kettles) {
const report = [];
for (let i = 0; i < kettles.length; i++) {
const entry = kettles[i];
if (entry.stock > 0) {
report.push(formatKettleListing(entry));
}
}
return report;
}
function removeDiscontinuedModel(kettles, targetModel) {
const index = kettles.findIndex((k) => k.model === targetModel);
if (index !== -1) {
kettles.pop(index);
}
return kettles;
}
const availableKettles = auditStock(kettleInventory);
const updatedInventory = removeDiscontinuedModel(kettleInventory, "Glass Steep");
console.log(availableKettles);
console.log(updatedInventory);
Reply with what is broken and how you would fix it.
The removeDiscontinuedModel function uses pop() incorrectly. pop() removes the last element, not the element at a specific index.
To remove an element by index, you should use splice(index, 1) instead.
Spot the Bug answer: The removeDiscontinuedModel function uses pop() instead of splice() to remove an item at a specific index.
The fix:
Change `kettles.pop(index);` to `kettles.splice(index, 1);`.
Why:
pop() removes the last element of an array, regardless of the argument passed to it. To remove an element at a specific index, splice(index, 1) should be used, where index is the starting position and 1 is the number of elements to remove.
First-answer leaderboard
- @emmawalter5 - 15 (firsts)

- @Apexcodes - 9 (firsts)
- @kirupa - 8 (firsts)
- @PipMuse - 3 (firsts)
- @adnanahmed - 2 (firsts)
The pop() misuse is the headline bug, but there’s a subtle follow-on worth flagging: availableKettles is captured before removeDiscontinuedModel runs, so the audit snapshot still reflects the original stock count. That’s fine if the intent is a historical report, but misleading if callers expect it to track the updated inventory. Also, since splice mutates in place and the function returns the same reference, kettleInventory and updatedInventory are the same array. Splitting this into a pure copy plus splice on the copy would keep the audit and mutation independent.
Nice catch, farahednafionah1375 — that second part is the bit most people would miss. The stale audit snapshot is exactly the kind of thing that looks fine in a quick test and then quietly misleads someone later.
Your point about the aliasing is spot on too. If the audit is meant to be a before picture, naming it that way would make the intent obvious and stop the next reader from assuming it should track the updated list.