-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbooks.js
More file actions
40 lines (34 loc) · 1.04 KB
/
Copy pathbooks.js
File metadata and controls
40 lines (34 loc) · 1.04 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
39
40
function findAuthorById(authors, id) {
return authors.find((author) => author.id === id);
}
function findBookById(books, id) {
return books.find((book) => book.id === id);
}
//helper function for partitionBooksByBorrowedStatus(books)
function returnedBook(book) {
return book.borrows.every((borrow) => borrow.returned);
}
function partitionBooksByBorrowedStatus(books) {
const booksCheckedOut = [];
const booksReturned = [];
books.map((book) => {
returnedBook(book) ? booksReturned.push(book) : booksCheckedOut.push(book);
});
const bookLog = [[...booksCheckedOut], [...booksReturned]];
return bookLog;
}
function getBorrowersForBook(book, accounts) {
const borrowers = [];
book.borrows.map((borrow) => {
let foundAccount = accounts.find((account) => account.id === borrow.id);
foundAccount = {...foundAccount, returned: borrow.returned};
borrowers.push(foundAccount);
});
return borrowers.slice(0, 10);
}
module.exports = {
findAuthorById,
findBookById,
partitionBooksByBorrowedStatus,
getBorrowersForBook,
};