Advertisement
nikolayneykov

Untitled

Apr 11th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve (params) {
  2.   let dictionary = {}
  3.   let wordDescriptionPairs = params[0].split(' | ')
  4.  
  5.   for (let pair of wordDescriptionPairs) {
  6.     let tokens = pair.split(': ')
  7.     let word = tokens[0]
  8.     let description = tokens[1]
  9.  
  10.     if (!dictionary.hasOwnProperty(word)) {
  11.       dictionary[word] = []
  12.     }
  13.  
  14.     dictionary[word].push(description)
  15.   }
  16.  
  17.   let words = params[1].split(' | ')
  18.  
  19.   for (let word of words) {
  20.     if (dictionary.hasOwnProperty(word)) {
  21.       console.log(word)
  22.  
  23.       let descriptions = dictionary[word].sort((a, b) => b.length - a.length)
  24.  
  25.       for (let description of descriptions) {
  26.         console.log(` -${description}`)
  27.       }
  28.     }
  29.   }
  30.  
  31.   let command = params[2]
  32.  
  33.   if (command === 'List') {
  34.     let wordsToList = Object.keys(dictionary).sort()
  35.  
  36.     console.log(wordsToList.join(' '))
  37.   }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement