-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_5.js
More file actions
38 lines (32 loc) · 1.78 KB
/
script_5.js
File metadata and controls
38 lines (32 loc) · 1.78 KB
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
let books = [
{ title: 'The Great Gatsby', id: 133712, rented: 39 },
{ title: 'In Search of Lost Time', id: 237634, rented: 28 },
{ title: 'Pride & Prejudice', id: 873495, rented: 67 },
{ title: 'The Brothers Karamazov', id: 450911, rented: 55 },
{ title: 'In the Forests of Siberia', id: 8376365, rented: 15 },
{ title: 'Why I Ate My Father', id: 450911, rented: 45 },
{ title: "We'll Kill All the Ugly Ones", id: 67565, rented: 36 },
{ title: 'Brave New World', id: 88847, rented: 58 },
{ title: 'The Disappearance', id: 364445, rented: 33 },
{ title: 'Only the Moon Knows', id: 63541, rented: 43 },
{ title: 'Journey to the Center of the Earth', id: 4656388, rented: 38 },
{ title: 'War and Peace', id: 748147, rented: 19 }
];
// 1) Have all books been borrowed at least once?
const allBorrowed = books.every(b => b.rented > 0);
console.log("Have all books been borrowed at least once? ", allBorrowed);
// 2) Which book has been borrowed the most?
const mostBorrowed = books.reduce((best, b) => (b.rented > best.rented ? b : best), books[0]);
console.log("Most borrowed book:", mostBorrowed);
// 3) Which book has been borrowed the least?
const leastBorrowed = books.reduce((worst, b) => (b.rented < worst.rented ? b : worst), books[0]);
console.log("Least borrowed book:", leastBorrowed);
// 4) Find the book with ID: 873495
const book873495 = books.find(b => b.id === 873495);
console.log("Book with ID 873495:", book873495);
// 5) Delete the book with ID: 133712
books = books.filter(b => b.id !== 133712);
console.log("List after deleting ID 133712:", books);
// 6) Sort the books alphabetically (excluding deleted one)
const sortedBooks = [...books].sort((a, b) => a.title.localeCompare(b.title));
console.log("Books sorted by title:", sortedBooks);