Todorov_Stanimir

02. On the Way to Annapurna Final Exam - 14 April 2019 Group

May 22nd, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function onTheWayToAnnapurna(input) {
  2.     let output = {};
  3.     let curCommand = input.shift();
  4.  
  5.     while (curCommand !== 'END') {
  6.         let [command, store, items] = curCommand.split('->');
  7.         if (command === 'Add') {
  8.             if (!Object.keys(output).includes(store)) {
  9.                 output[store] = []
  10.             }
  11.             items.split(',').forEach(element => {
  12.                 output[store].push(element);
  13.             });
  14.         } else if (command === 'Remove') {
  15.             if (Object.keys(output).includes(store)) {
  16.                 delete(output[store]);
  17.             }
  18.         }
  19.         curCommand = input.shift();
  20.     }
  21.     output = Object.entries(output);
  22.     output.sort((a, b) => {
  23.         if (a[1].length > b[1].length) {
  24.             return -1;
  25.         } else if (a[1].length < b[1].length) {
  26.             return 1;
  27.         } else {
  28.             if (a[0] > b[0]) {
  29.                 return -1;
  30.             } else if (a[0] < b[0]) {
  31.                 return 1
  32.             }
  33.         }
  34.     });
  35.     console.log('Stores list:');
  36.     output.forEach(element => {
  37.         console.log(element[0]);
  38.         element[1].forEach(element1 => {
  39.             console.log(`<<${element1}>>`);
  40.  
  41.         });
  42.     });
  43. }
Advertisement
Add Comment
Please, Sign In to add comment