Advertisement
tetris555

make-a-dictionary

Nov 2nd, 2022
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.     class Term {
  3.         constructor(word, definition) {
  4.             this.word = word;
  5.             this.definition = definition;
  6.         }
  7.     }
  8.  
  9.     const words = [];
  10.     const definitions = [];
  11.    
  12.     for (const line of arr) {
  13.         const currentTerm = JSON.parse(line);
  14.         const currentWord = Object.keys(currentTerm)[0];
  15.         const currentDefinition = Object.values(currentTerm)[0];
  16.        
  17.         const termIndex = words.indexOf(currentWord);
  18.         if (termIndex > -1) {
  19.             definitions[termIndex] = currentDefinition;
  20.         } else {
  21.             words.push(currentWord);
  22.             definitions.push(currentDefinition);
  23.         }
  24.     }
  25.    
  26.     const terms = [];
  27.  
  28.     for (let i = 0; i < words.length; i++) {
  29.         terms.push(new Term(words[i], definitions[i]));
  30.     }
  31.  
  32.     terms.sort((a, b) => a.word.localeCompare(b.word)).forEach(term => {
  33.         console.log(`Term: ${term.word} => Definition: ${term.definition}`);
  34.     })
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement