kstoyanov

04. Radical Marketing js exam

Jul 31st, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(args) {
  2.   const log = new Map();
  3.   const subscriptions = new Map();
  4.   let count = 1;
  5.  
  6.   args.forEach((el) => {
  7.     const currentCommand = el.split('-');
  8.  
  9.     if (currentCommand.length === 2) {
  10.       if (log.has(currentCommand[0]) && log.has(currentCommand[1])) {
  11.         log.get(currentCommand[1]).add(currentCommand[0]);
  12.         subscriptions.get(currentCommand[0]).add(currentCommand[1]);
  13.       }
  14.     } else if (!log.has(currentCommand[0])) {
  15.       log.set(currentCommand[0], new Set());
  16.       subscriptions.set(currentCommand[0], new Set());
  17.     }
  18.   });
  19.  
  20.   const sortedLog = new Map([...log.entries()].sort((firstEntry, secondEntry) => {
  21.     const firstEntryName = firstEntry[0];
  22.     const firstEntrySubscribers = firstEntry[1].size;
  23.  
  24.     const secondEntryName = secondEntry[0];
  25.     const secondEntrySubscribers = secondEntry[1].size;
  26.  
  27.     let result = secondEntrySubscribers - firstEntrySubscribers;
  28.  
  29.     if (result === 0) {
  30.       const firstEntrySubscriptions = subscriptions.get(firstEntryName).size;
  31.       const secondEntrySubscriptions = subscriptions.get(secondEntryName).size;
  32.  
  33.       result = secondEntrySubscriptions - firstEntrySubscriptions;
  34.     }
  35.  
  36.     return result;
  37.   }));
  38.  
  39.   const importPerson = [...sortedLog.entries()][0];
  40.   console.log(importPerson[0]);
  41.  
  42.  
  43.   importPerson[1].forEach((e) => {
  44.     console.log(`${count}. ${e}`);
  45.     count++;
  46.   });
  47. }
Add Comment
Please, Sign In to add comment