Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. groupedByAuthorBooks = _.groupBy(books, 'author');
  2. groupedByAuthorBooksKeys = _.keys(groupedByAuthorBooks);
  3. console.log('\nAverage books price for each author:');
  4.  
  5. for(var i = 0; i < groupedByAuthorBooksKeys.length; i++) {
  6.     var author = groupedByAuthorBooksKeys[i];
  7.     console.log('Author: ' + author);
  8.     console.log('Average books price: ' + (averageBookPriceByAuthor(groupedByAuthorBooks[author]) / groupedByAuthorBooks[groupedByAuthorBooksKeys[i]].length).toFixed(2));
  9. }
  10.  
  11. function averageBookPriceByAuthor (author) {
  12.     var totalBooksPrice = 0;
  13.  
  14.     for(var i = 0; i < author.length; i++) {
  15.         totalBooksPrice += Number(author[i].price.replace(',', '.'))
  16.     }
  17.  
  18.     return totalBooksPrice;
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement