5. You want to create a DELETE route that removes a book by its ID from a nested data structure where books are grouped by genre. Which approach correctly handles deletion and sends a 404 if the book is not found?
const library = {
fiction: [{id: 1, title: 'A'}, {id: 2, title: 'B'}],
nonFiction: [{id: 3, title: 'C'}]
};
app.delete('/books/:id', (req, res) => {
const id = Number(req.params.id);
let found = false;
for (const genre in library) {
const index = library[genre].findIndex(book => book.id === id);
if (index !== -1) {
library[genre].splice(index, 1);
found = true;
break;
}
}
if (found) {
res.status(200).send('Book deleted');
} else {
res.status(404).send('Book not found');
}
});