Advertisement
vladovip

06. Dictionary _JS Objects Exercise

Feb 26th, 2022 (edited)
1,222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function dictionary(input){
  2.     let dict = {};
  3.     for (let element of input){
  4.         let obj = JSON.parse(element);
  5.         dict = Object.assign(dict, obj);        
  6.     }
  7.        
  8.     let sortedKeys = Object.keys(dict);
  9.     sortedKeys.sort((a, b) => a.localeCompare(b));  
  10.      
  11.     for (let term of sortedKeys) {
  12.         let definition = dict[term];            
  13.         console.log(`Term: ${term} => Definition: ${definition}`);
  14.     }
  15. }
  16.  
  17. dictionary(
  18.     [
  19.     '{"Coffee":"A hot drink made from the roasted and ground seeds (coffee beans) of a tropical shrub."}',
  20.     '{"Bus":"A large motor vehicle carrying passengers by road, typically one serving the public on a fixed route and for a fare."}',
  21.     '{"Boiler":"A fuel-burning apparatus or container for heating water."}',
  22.     '{"Tape":"A narrow strip of material, typically used to hold or fasten something."}',
  23.     '{"Microphone":"An instrument for converting sound waves into electrical energy variations which may then be amplified, transmitted, or recorded."}'
  24.     ]
  25.     );
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement