Advertisement
Lulunga

Final Exam 02. On the Way to Annapurna

Aug 1st, 2019
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.    let collection = {};
  3.  
  4.    function add(storeName, items) {
  5.       if (!collection[storeName]) {
  6.          collection[storeName] = [];
  7.       }
  8.       let currentItems = collection[storeName];
  9.       collection[storeName] = currentItems.concat(items);
  10.    }
  11.  
  12.    function remove(storeName) {
  13.       if (collection[storeName]) {
  14.          delete collection[storeName];
  15.       }
  16.    }
  17.  
  18.    for (let line of input) {
  19.       if (line === 'END') {
  20.          break;
  21.       }
  22.       let [command, store, otherItems] = line.split('->');
  23.       if (command === 'Add') {
  24.          if (otherItems.includes(',')) {
  25.             otherItems = otherItems.split(',');
  26.          }
  27.          add(store, otherItems);
  28.       } else if (command === 'Remove') {
  29.          remove(store);
  30.       }
  31.    }
  32.    console.log(`Stores list:`);
  33.    let sorted = Object.entries(collection)
  34.       .sort((a, b) => b[1].length - a[1].length || b[0].localeCompare(a[0]))
  35.       .forEach(element => {
  36.          console.log(`${element[0]}`);
  37.          element[1].forEach(item => {
  38.             console.log(`<<${item}>>`);
  39.          });
  40.       });
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement