Advertisement
bebo231312312321

Untitled

Mar 18th, 2023
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function bookShelf(input) {
  2.     let shelf = {};
  3.     for (let line of input){
  4.         if(line.includes("->")){
  5.        let [iD, genre] = line.split(' -> ')
  6.         if(shelf[iD] === undefined) shelf[iD] = {[genre]: [] };  
  7.     }else{
  8.            let [bookName, genre] = line.split(", ")//
  9.            for (let element in shelf){
  10.              if(shelf[element].hasOwnProperty(genre))
  11.              shelf[element][genre].push(bookName)
  12.              }
  13.             }
  14.         }
  15.         Object.keys(shelf).sort((a,b)=>Object.entries(shelf[b])[0][1].length - Object.entries(shelf[a])[0][1].length).forEach(el=>{
  16.             console.log(`${el} ${Object.keys(shelf[el])}: ${Object.values(shelf[el])[0].length}`)
  17.        
  18.             Object.values(shelf[el]).map(x=>{x.sort((a,b)=> a.localeCompare(b)).map(el=> console.log(`--> ${el}`))
  19.             })
  20.         })
  21.     }
  22. bookShelf([
  23.     "1 -> history",
  24.     "1 -> action",
  25.     "Death in Time: Criss Bell, mystery",
  26.     "2 -> mystery",
  27.     "3 -> sci-fi",
  28.     "Child of Silver: Bruce Rich, mystery",
  29.     "Hurting Secrets: Dustin Bolt, action",
  30.     "Future of Dawn: Aiden Rose, sci-fi",
  31.     "Lions and Rats: Gabe Roads, history",
  32.     "2 -> romance",
  33.     "Effect of the Void: Shay B, romance",
  34.     "Losing Dreams: Gail Starr, sci-fi",
  35.     "Name of Earth: Jo Bell, sci-fi",
  36.     "Pilots of Stone: Brook Jay, history",
  37. ]);
  38. console.log("----------------------");
  39. bookShelf([
  40.     "1 -> mystery",
  41.     "2 -> sci-fi",
  42.     "Child of Silver: Bruce Rich, mystery",
  43.     "Lions and Rats: Gabe Roads, history",
  44.     "Effect of the Void: Shay B, romance",
  45.     "Losing Dreams: Gail Starr, sci-fi",
  46.     "Name of Earth: Jo Bell, sci-fi",
  47. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement