Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- groupedByAuthorBooks = _.groupBy(books, 'author');
- groupedByAuthorBooksKeys = _.keys(groupedByAuthorBooks);
- console.log('\nAverage books price for each author:');
- for(var i = 0; i < groupedByAuthorBooksKeys.length; i++) {
- var author = groupedByAuthorBooksKeys[i];
- console.log('Author: ' + author);
- console.log('Average books price: ' + (averageBookPriceByAuthor(groupedByAuthorBooks[author]) / groupedByAuthorBooks[groupedByAuthorBooksKeys[i]].length).toFixed(2));
- }
- function averageBookPriceByAuthor (author) {
- var totalBooksPrice = 0;
- for(var i = 0; i < author.length; i++) {
- totalBooksPrice += Number(author[i].price.replace(',', '.'))
- }
- return totalBooksPrice;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement