Todorov_Stanimir

01. Dictionary from Tech.Fundamentals Final Exam-06.04.2019

May 16th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function dictionary(input) {
  2.  
  3.     let wordsAndDefinitions = input.shift().split(' | ');
  4.     let dictionary = new Map();
  5.  
  6.     for (let wordAndDefinition of wordsAndDefinitions) {
  7.         let [word, definition] = wordAndDefinition.split(': ');
  8.         if (dictionary.has(word)) {
  9.             let currentDefinitions = dictionary.get(word);
  10.             currentDefinitions.push(definition);
  11.             dictionary.set(word, currentDefinitions);
  12.         } else {
  13.             dictionary.set(word, [definition]);
  14.         }
  15.     }
  16.     let wordsForChecking = input.shift().split(' | ');
  17.     for (let checkedWord of wordsForChecking) {
  18.         if (dictionary.has(checkedWord)) {
  19.             console.log(checkedWord);
  20.             let definitions = dictionary.get(checkedWord).sort((a, b) => b.length - a.length);
  21.             definitions.forEach(element => {
  22.                 console.log(` -${element}`);
  23.             });;
  24.         }
  25.     }
  26.  
  27.     let lastCommand = input.shift();
  28.  
  29.     if (lastCommand === 'List') {
  30.         console.log(Array.from(dictionary.keys()).sort((a, b) => a.localeCompare(b)).join(' '));
  31.     }
  32. }
  33.  
  34. dictionary(['programmer: an animal, which turns coffee into code | developer: a magician',
  35.     'Pesho | Gosho',
  36.     'List'
  37. ]);
  38.  
  39. dictionary(['tackle: the equipment required for a task or sport | code: write code for a computer program | bit: a small piece, part, or quantity of something | tackle: make determined efforts to deal with a problem | bit: a short time or distance',
  40.     'bit | code | tackle',
  41.     'End'
  42. ]);
Advertisement
Add Comment
Please, Sign In to add comment