maymunskoa

Book Library (Archive Objects & Classes)

Apr 1st, 2020
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.   class Book {
  3.     constructor(title, author, publisher, releaseDate, isbn, price) {
  4.       this.title = title;
  5.       this.author = author;
  6.       this.publisher = publisher;
  7.       this.date = releaseDate;
  8.       this.isbn = isbn;
  9.       this.price = price;
  10.     }
  11.   }
  12.  
  13.   let data = input.slice(0).filter(x => x !== "");
  14.   const count = Number(data.shift());
  15.   let library = {};
  16.   for (const line of data) {
  17.     let book = new Book(...line.split(" "));
  18.     if (!library[book.author]) {
  19.       library[book.author] = 0;
  20.     }
  21.     library[book.author] += Number(book.price);
  22.   }
  23.  
  24.   Object.entries(library)
  25.     .sort((a, b) => {
  26.       if (a[1] == b[1]) {
  27.         return a[0].localeCompare(b[0]);
  28.       }
  29.       return b[1] - a[1];
  30.     })
  31.     .forEach(pair => {
  32.       console.log(`${pair[0]} -> ${pair[1].toFixed(2)}`);
  33.     });
  34. }
Advertisement
Add Comment
Please, Sign In to add comment