Advertisement
Guest User

Untitled

a guest
Jan 6th, 2021
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function bookShelf(input) {
  2.   let shelvesList = new Map();
  3.   let bookList = new Map();
  4.   for (const line of input) {
  5.     if (line.includes('->')) {
  6.       let [id, genre] = line.split('->').map(str => str.trim());
  7.       if (![...shelvesList.values()].includes(id)) {
  8.         shelvesList.set(genre, id);
  9.         bookList.set(genre, []);
  10.       }
  11.     } else {
  12.       let [bookTitle, bookAuthor, genre] = line.split(/[:,]+/).map(str => str.trim());
  13.       if (bookList.has(genre)) {
  14.         bookList.get(genre).push({bookTitle: bookTitle, bookAuthor: bookAuthor});
  15.       }
  16.     }
  17.   }
  18.  
  19.   [...bookList.entries()]
  20.     .sort((a, b) => b[1].length - a[1].length)
  21.     .forEach((shelf) => {
  22.       let [genre, books] = shelf;
  23.       books.sort((a, b) => a.bookTitle.localeCompare(b.bookTitle));
  24.       console.log(`${shelvesList.get(genre)} ${genre}: ${books.length}`);
  25.       for (const book of books) {
  26.         console.log(`--> ${book.bookTitle}: ${book.bookAuthor}`);
  27.       }
  28.     });
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement