Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function bookShelf(array) {
- let objID = {};
- let objBooks = {};
- for (let el of array) {
- if (el.includes("->")) {
- let [ID, genre] = el.split(" -> ");
- if (objID.hasOwnProperty(ID) == false) {
- objID[ID] = genre;
- objBooks[genre] = [];
- }
- }
- if (el.includes(": ")) {
- let [bookTitle, tokens] = el.split(": ");
- let [author, bookGenre] = tokens.split(", ");
- for (let key in objID) {
- if (objID[key] == bookGenre) {
- objBooks[bookGenre].push([bookTitle, author]);
- }
- }
- }
- }
- let sortedBooks = Object.entries(objBooks).sort(
- (a, b) => b[1].length - a[1].length
- );
- for (let [el, bookAndAuthor] of sortedBooks) {
- for (let key in objID) {
- if (objID[key] === el) {
- console.log(`${key} ${el}: ${bookAndAuthor.length}`);
- for (let [book, author] of bookAndAuthor) {
- console.log(`--> ${book}: ${author}`);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement