Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function annapurna(array = []) {
- let obj = {};
- let [command, store, product] = array.shift().split('->');
- while (command !== 'END') {
- switch (command) {
- case 'Add':
- let items = product.split(',');
- if (!obj.hasOwnProperty(store)) {
- obj[store] = [];
- }
- // for (let i = 0; i < items.length; i++) {
- // obj[store].push(items[i]);
- // }
- obj[store] = obj[store].concat(items);
- break;
- case 'Remove':
- if (obj.hasOwnProperty(store)) {
- delete obj[store];
- }
- break;
- }
- [command, store, product] = array.shift().split('->');
- }
- // sort array
- let entries = Object.entries(obj).sort((a, b) => {
- if (a[1].length !== b[1].length) {
- return b[1].length - a[1].length;
- } else {
- return b[0].localeCompare(a[0]);
- }
- });
- console.log('Stores list:');
- for (let i = 0; i < entries.length; i++) {
- console.log(entries[i][0]);
- for (let j = 0; j < entries[i][1].length; j++) {
- console.log(`<<${entries[i][1][j]}>>`);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement