Advertisement
Guest User

Boooyaaa

a guest
Apr 2nd, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function dictionary(arr) {
  2.     let definitions = arr[0].split(" | ");
  3.     let definitionsToPrint = arr[1].split(" | ").sort((a, b) => a.localeCompare(b));
  4.     let lastCommand = arr[2];
  5.  
  6.     let dictionary = {};
  7.  
  8.     definitions.forEach(element => {
  9.         dictionary = dictionaryPopulation(dictionary, element);
  10.         for (let word in dictionary) {
  11.             dictionary[word].sort((a, b) => b.length - a.length || a.localeCompare(b));
  12.         };
  13.     });
  14.  
  15.     definitionsToPrint.forEach(definition => {
  16.         if (dictionary[definition] !== undefined) {
  17.             console.log(`${definition}\n -${dictionary[definition].join(`\n -`)}`);
  18.  
  19.         }
  20.     })
  21.  
  22.     if (lastCommand === "End") return;
  23.     if (lastCommand === "List") console.log(Object.keys(dictionary).sort((a, b) => a.localeCompare(b)).join(" "));
  24.  
  25.     function dictionaryPopulation(obj, input) {
  26.         let [word, definition] = input.split(": ");
  27.         if (obj[word] === undefined) {
  28.             obj[word] = [];
  29.         }
  30.         obj[word].push(definition);
  31.         return obj;
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement