-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.js
36 lines (27 loc) · 957 Bytes
/
store.js
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
const getBooksByYear = (books, year) => {
return books.filter((book) => {
const date = book.date_read || book.date_added;
return date.getFullYear() === year;
});
};
const getAveragePages = books => {
const booksWithPages = books.filter(book => book.pages);
return Math.round(booksWithPages.reduce((sum, book) => {
return sum + book.pages;
}, 0) / booksWithPages.length)
}
const getPages = (books, averagePages) => books.map(book => book.pages || averagePages);
module.exports = function (books) {
const readBooks = books.filter((book) => book.bookshelves === "read");
console.log("Total books ", books.length);
console.log(
"Total read books ",
readBooks.length
);
//calculate average to fill blank values
const averagePages = getAveragePages(readBooks);
return {
getPagesByYear: (year) => getPages(getBooksByYear(readBooks, year), averagePages),
getPages: () => getPages(readBooks, averagePages)
};
};