-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhome.js
More file actions
60 lines (54 loc) · 1.71 KB
/
Copy pathhome.js
File metadata and controls
60 lines (54 loc) · 1.71 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
49
50
51
52
53
54
55
56
57
58
59
60
function getTotalBooksCount(books) {
return books.length;
}
function getTotalAccountsCount(accounts) {
return accounts.length;
}
function getBooksBorrowedCount(books) {
return books.reduce((total, book) => {
return (total + book.borrows.reduce((last, borrow) => {
return last + !borrow.returned ? 1 : 0;
}, 0)
);
}, 0);
}
function getMostCommonGenres(books) {
const genres = [];
books.map((book) => {
const foundGenre = genres.find((genre) => genre.name === book.genre);
if (foundGenre) foundGenre.count++;
else genres.push({name: book.genre, count: 1});
});
genres.sort((genreA, genreB) => (genreB.count - genreA.count));
return genres.slice(0, 5);
}
function getMostPopularBooks(books) {
const mostPopular = [];
books.map((book) => {
mostPopular.push({name: book.title, count: book.borrows.length});
});
mostPopular.sort((bookA, bookB) => (bookB.count - bookA.count));
return mostPopular.slice(0,5);
}
function getMostPopularAuthors(books, authors) {
const popularAuthor = [];
authors.map((author) => {
const name = `${author.name.first} ${author.name.last}`;
const foundAuthor = popularAuthor.find((authorInfo) => authorInfo.name === name);
const count = books.reduce((total, book) => {
return total + (book.authorId === author.id ? book.borrows.length : 0)
}, 0);
if (foundAuthor) foundAuthor.count += count;
else popularAuthor.push({name: name, count: count});
});
popularAuthor.sort((authorA, authorB) => authorB.count - authorA.count);
return popularAuthor.slice(0,5);
}
module.exports = {
getTotalBooksCount,
getTotalAccountsCount,
getBooksBorrowedCount,
getMostCommonGenres,
getMostPopularBooks,
getMostPopularAuthors,
};