Spot the bug - #184: Tea Kettle Inventory

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.