Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function bookShelf(input) {
- let shelf = {};
- for (let line of input){
- if(line.includes("->")){
- let [iD, genre] = line.split(' -> ')
- if(shelf[iD] === undefined) shelf[iD] = {[genre]: [] };
- }else{
- let [bookName, genre] = line.split(", ")//
- for (let element in shelf){
- if(shelf[element].hasOwnProperty(genre))
- shelf[element][genre].push(bookName)
- }
- }
- }
- Object.keys(shelf).sort((a,b)=>Object.entries(shelf[b])[0][1].length - Object.entries(shelf[a])[0][1].length).forEach(el=>{
- console.log(`${el} ${Object.keys(shelf[el])}: ${Object.values(shelf[el])[0].length}`)
- Object.values(shelf[el]).map(x=>{x.sort((a,b)=> a.localeCompare(b)).map(el=> console.log(`--> ${el}`))
- })
- })
- }
- bookShelf([
- "1 -> history",
- "1 -> action",
- "Death in Time: Criss Bell, mystery",
- "2 -> mystery",
- "3 -> sci-fi",
- "Child of Silver: Bruce Rich, mystery",
- "Hurting Secrets: Dustin Bolt, action",
- "Future of Dawn: Aiden Rose, sci-fi",
- "Lions and Rats: Gabe Roads, history",
- "2 -> romance",
- "Effect of the Void: Shay B, romance",
- "Losing Dreams: Gail Starr, sci-fi",
- "Name of Earth: Jo Bell, sci-fi",
- "Pilots of Stone: Brook Jay, history",
- ]);
- console.log("----------------------");
- bookShelf([
- "1 -> mystery",
- "2 -> sci-fi",
- "Child of Silver: Bruce Rich, mystery",
- "Lions and Rats: Gabe Roads, history",
- "Effect of the Void: Shay B, romance",
- "Losing Dreams: Gail Starr, sci-fi",
- "Name of Earth: Jo Bell, sci-fi",
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement