codex-lv4-may-2025

Level Navigation: 1 2 3 (4ℹ️) (5ℹ️) 6 7 8 9 10 11 12 13 14⚡ 15⚡ (16ℹ️) 17 18 19 20 21 22 23 24 25 26⚡ 27⚡ 28⚡ 29 30 31 32 33 34 35 36 37 38 39⚡ 40⚡

Level 24: Implement DELETE /items/:id

Show Me: complete DELETE route

app.delete('/items/:id', (req, res) => {
  
  console.log("Deleting " + req.params.id)

  // Find the item first to check if it exists
  const item = itemsStorage.find((entry) => entry.id === req.params.id);
  
  // If not found, return 404
  if (!item) {
    return res.status(404).json({ error: 'Item not found' });
  }
  
  // TODO: Delete the item from your storage.
  
  
  // Return success response
  res.status(200).json({ message: 'Item deleted successfully' });
});
Show Me: delete using filter pattern

const item = itemsStorage.find((entry) => entry.id === req.params.id);
if (!item) return res.status(404).json({ error: 'Item not found' });

// Use filter to create a new array without the deleted item
// The filter pattern keeps all items where id does NOT match
itemsStorage = itemsStorage.filter((entry) => entry.id !== req.params.id);
res.status(200).json({ message: 'Item deleted successfully' });