-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccounts.js
More file actions
48 lines (42 loc) · 1.23 KB
/
Copy pathaccounts.js
File metadata and controls
48 lines (42 loc) · 1.23 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
41
42
43
44
45
46
47
48
function findAccountById(accounts, id) {
return accounts.find((account) => account.id === id);
}
function sortAccountsByLastName(accounts) {
return accounts.sort((accountA, accountB) => {
return accountA.name.last < accountB.name.last ? -1 : 1;
});
}
// helper function for getTotalNumberOfBorrows()
function individualBookBorrows(book, id) {
let total = 0;
for(let key in book.borrows) {
if(book.borrows[key].id === id) {
total++;
}
}
return total;
}
function getTotalNumberOfBorrows(account, books) {
return books.reduce((total, book) => {
return total + individualBookBorrows(book, account.id);
}, 0);
}
function getBooksPossessedByAccount(account, books, authors) {
const notReturnedBooks = books.filter((book) => {
return book.borrows.some((borrow) => {
return borrow.returned === false && borrow.id === account.id;
});
});
const bookMap = notReturnedBooks.map((book) => {
const {borrows, ...other} = book;
const auth = authors.find((author) => author.id === book.authorId);
return {...other, author: auth, borrows};
});
return bookMap;
}
module.exports = {
findAccountById,
sortAccountsByLastName,
getTotalNumberOfBorrows,
getBooksPossessedByAccount,
};