t_sh0w

01. Dictionary 90/100

Apr 2nd, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input = []) {
  2.   let wordsAndDescriptions = input.shift().split(" | ");
  3.   let words = input.shift().split(" | ");
  4.   let command = input.shift();
  5.   let dictionary = {};
  6.   let keys = [];
  7.  
  8.   for (const current of wordsAndDescriptions) {
  9.     let [word, description] = current.split(": ");
  10.  
  11.     if (!Object.keys(dictionary).includes(word)) {
  12.       dictionary[word] = [];
  13.       dictionary[word].push(description);
  14.       keys.push(word);
  15.     } else {
  16.       dictionary[word].push(description);
  17.     }
  18.   }
  19.  
  20.   if (command === "End") {
  21.     let entries = Object.entries(dictionary).sort((a, b) => a[0].localeCompare(b[0]));
  22.     for (const [key, values] of entries) {
  23.       values.sort((a, b) => b.length - a.length);
  24.       console.log(key);
  25.       values.forEach(value => console.log(` -${value}`));
  26.     }
  27.   } else if (command === "List") {
  28.     let sortedKeys = keys.sort((a, b) => a.localeCompare(b));
  29.     console.log(sortedKeys.join(" "));
  30.   }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment