Advertisement
nikolayneykov

Untitled

Apr 7th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve (arr) {
  2.   let dictionary = {}
  3.   let [input, words, output] = arr
  4.  
  5.   input = input.split(' | ')
  6.  
  7.   for (const line of input) {
  8.     let [word, meaning] = line.split(': ')
  9.  
  10.     if (dictionary.hasOwnProperty(word)) {
  11.       let definitions = dictionary[word]
  12.       definitions.push(meaning)
  13.       dictionary[word] = definitions
  14.     } else {
  15.       let definition = []
  16.       definition.push(meaning)
  17.       dictionary[word] = definition
  18.     }
  19.   }
  20.  
  21.   let result = Object.getOwnPropertyNames(dictionary).sort()
  22.  
  23.   if (output === 'List') {
  24.     console.log(result.join(' '))
  25.   } else if (output === 'End') {
  26.     result = result.filter(x => words.split(' ').includes(x))
  27.     for (const word of result) {
  28.       console.log(word)
  29.  
  30.       let defs = dictionary[word]
  31.         .sort((a, b) => a.length < b.length)
  32.         .map(x => console.log(` -${x}`))
  33.     }
  34.   }
  35. }
  36.  
  37. solve([
  38.   'programmer: an animal, which turns coffee into code | developer: a magician',
  39.   'Pesho | Gosho',
  40.   'List'
  41. ])
  42.  
  43. solve([
  44.   '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',
  45.   'bit | code | tackle',
  46.   'End'
  47. ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement