Advertisement
dchukova

Untitled

Mar 31st, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. function annapurna(array = []) {
  2. let obj = {};
  3. let [command, store, product] = array.shift().split('->');
  4. while (command !== 'END') {
  5. switch (command) {
  6. case 'Add':
  7. let items = product.split(',');
  8. if (!obj.hasOwnProperty(store)) {
  9. obj[store] = [];
  10. }
  11. // for (let i = 0; i < items.length; i++) {
  12. // obj[store].push(items[i]);
  13. // }
  14. obj[store] = obj[store].concat(items);
  15. break;
  16. case 'Remove':
  17. if (obj.hasOwnProperty(store)) {
  18. delete obj[store];
  19. }
  20. break;
  21. }
  22. [command, store, product] = array.shift().split('->');
  23. }
  24. // sort array
  25. let entries = Object.entries(obj).sort((a, b) => {
  26. if (a[1].length !== b[1].length) {
  27. return b[1].length - a[1].length;
  28. } else {
  29. return b[0].localeCompare(a[0]);
  30. }
  31. });
  32.  
  33. console.log('Stores list:');
  34.  
  35. for (let i = 0; i < entries.length; i++) {
  36.  
  37. console.log(entries[i][0]);
  38.  
  39. for (let j = 0; j < entries[i][1].length; j++) {
  40. console.log(`<<${entries[i][1][j]}>>`);
  41. }
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement