Advertisement
maymunskoa

06. Book Library Modification (Archive Objects & Classes)

Apr 2nd, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.   class Book {
  3.     constructor(title, author, publisher, releaseDate, isbn, price) {
  4.       this.title = title;
  5.       this.author = author;
  6.       this.publisher = publisher;
  7.       this.date = releaseDate;
  8.       this.isbn = isbn;
  9.       this.price = price;
  10.     }
  11.   }
  12.  
  13.   let data = input.slice(0).filter(x => x !== "");
  14.   const count = Number(data.shift());
  15.   const date = data.pop();
  16.   let [day, month, year] = date.split(".").map(Number);
  17.   let library = {};
  18.   for (const line of data) {
  19.     let book = new Book(...line.split(" "));
  20.     library[book.title] = book.date.split(".").map(Number);
  21.   }
  22.  
  23.   let result = Object.entries(library);
  24.   result
  25.     .filter(entry => {
  26.       if (entry[1][2] === year) {
  27.         if (entry[1][1] === month) {
  28.           if (entry[1][0] > day) {
  29.             return entry;
  30.           }
  31.         } else if (entry[1][1] > month) {
  32.           return entry;
  33.         }
  34.       } else if (entry[1][2] > year) {
  35.         return entry;
  36.       }
  37.     })
  38.     .sort((a, b) => {
  39.       if (b[1][2] === a[1][2]) {
  40.         if (b[1][1] === a[1][1]) {
  41.           if (a[1][0] === b[1][0]) {
  42.             return a[0].localeCompare(b[0]);
  43.           }
  44.           return a[1][0] - b[1][0];
  45.         }
  46.         return a[1][1] - b[1][1];
  47.       }
  48.       return a[1][2] - b[1][2];
  49.     })
  50.     .forEach(entry => {
  51.       let output = "";
  52.       output += entry[0];
  53.       output += " -> ";
  54.       output += entry[1]
  55.         .map(el => {
  56.           if (el < 10) {
  57.             return `0${el}`;
  58.           } else {
  59.             return `${el}`;
  60.           }
  61.         })
  62.         .join(".");
  63.       console.log(output);
  64.     });
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement