Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input = []) {
- let wordsAndDescriptions = input.shift().split(" | ");
- let words = input.shift().split(" | ");
- let command = input.shift();
- let dictionary = {};
- let keys = [];
- for (const current of wordsAndDescriptions) {
- let [word, description] = current.split(": ");
- if (!Object.keys(dictionary).includes(word)) {
- dictionary[word] = [];
- dictionary[word].push(description);
- keys.push(word);
- } else {
- dictionary[word].push(description);
- }
- }
- if (command === "End") {
- let entries = Object.entries(dictionary).sort((a, b) => a[0].localeCompare(b[0]));
- for (const [key, values] of entries) {
- values.sort((a, b) => b.length - a.length);
- console.log(key);
- values.forEach(value => console.log(` -${value}`));
- }
- } else if (command === "List") {
- let sortedKeys = keys.sort((a, b) => a.localeCompare(b));
- console.log(sortedKeys.join(" "));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment