Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let collection = {};
- function add(storeName, items) {
- if (!collection[storeName]) {
- collection[storeName] = [];
- }
- let currentItems = collection[storeName];
- collection[storeName] = currentItems.concat(items);
- }
- function remove(storeName) {
- if (collection[storeName]) {
- delete collection[storeName];
- }
- }
- for (let line of input) {
- if (line === 'END') {
- break;
- }
- let [command, store, otherItems] = line.split('->');
- if (command === 'Add') {
- if (otherItems.includes(',')) {
- otherItems = otherItems.split(',');
- }
- add(store, otherItems);
- } else if (command === 'Remove') {
- remove(store);
- }
- }
- console.log(`Stores list:`);
- let sorted = Object.entries(collection)
- .sort((a, b) => b[1].length - a[1].length || b[0].localeCompare(a[0]))
- .forEach(element => {
- console.log(`${element[0]}`);
- element[1].forEach(item => {
- console.log(`<<${item}>>`);
- });
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement