dilyana2001

dictionary

Aug 14th, 2021 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.     let dictionary = {};
  3.     let definitions = arr.shift();
  4.     definitions = definitions.split(' | ').forEach(line => {
  5.         let [word, definition] = line.split(': ');
  6.         if (!dictionary.hasOwnProperty(word)) {
  7.             dictionary[word] = [definition];
  8.         } else {
  9.             dictionary[word].push(definition);
  10.         }
  11.     })
  12.     let onTest = arr.shift();
  13.     let commandParser = {
  14.         'Test': () => {
  15.             onTest = onTest.split(' | ').forEach(w => {
  16.                 if (dictionary.hasOwnProperty(w)) {
  17.                     console.log(`${w}`);
  18.                     Object.entries(dictionary[w]).sort((a, b) => b[1].length - a[1].length).forEach(x => console.log(`-${x[1]}`));
  19.                 }
  20.             })
  21.         },
  22.         'Hand Over': () => {
  23.             console.log(Object.keys(dictionary).join(' '));
  24.         }
  25.     }
  26.     commandParser[arr.join()]();
  27. }
Add Comment
Please, Sign In to add comment