Advertisement
bebo231312312321

Untitled

Mar 18th, 2023
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function bookShelf(array) {
  2.   let objID = {};
  3.   let objBooks = {};
  4.  
  5.   for (let el of array) {
  6.     if (el.includes("->")) {
  7.       let [ID, genre] = el.split(" -> ");
  8.       if (objID.hasOwnProperty(ID) == false) {
  9.         objID[ID] = genre;
  10.         objBooks[genre] = [];
  11.       }
  12.     }
  13.     if (el.includes(": ")) {
  14.       let [bookTitle, tokens] = el.split(": ");
  15.       let [author, bookGenre] = tokens.split(", ");
  16.       for (let key in objID) {
  17.         if (objID[key] == bookGenre) {
  18.           objBooks[bookGenre].push([bookTitle, author]);
  19.         }
  20.       }
  21.     }
  22.   }
  23.  
  24.   let sortedBooks = Object.entries(objBooks).sort(
  25.     (a, b) => b[1].length - a[1].length
  26.   );
  27.  
  28.   for (let [el, bookAndAuthor] of sortedBooks) {
  29.     for (let key in objID) {
  30.       if (objID[key] === el) {
  31.         console.log(`${key} ${el}: ${bookAndAuthor.length}`);
  32.         for (let [book, author] of bookAndAuthor) {
  33.           console.log(`--> ${book}: ${author}`);
  34.         }
  35.       }
  36.     }
  37.   }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement