elena1234

How to create, fill in, sort and print map ( JavaScript)

Nov 29th, 2021 (edited)
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(...arguments) {
  2.     let firstResult = [];
  3.     let secondResult = new Map();
  4.     arguments.map((x) => {
  5.         let type = typeof x;
  6.         firstResult.push(`${type}: ${x}`);  // string: cat
  7.         if (!secondResult.has(type)) {
  8.             secondResult.set(type, 1); // string = 1
  9.         } else {
  10.             secondResult.set(type, secondResult.get(type) + 1); // number = 2
  11.         }
  12.     })
  13.  
  14.     printFirstResult(firstResult);
  15.     let sortedSecondResult = new Map([...secondResult.entries()].sort((a,b) => b[1] - a[1])) // sort by value
  16.     printSecondResult(sortedSecondResult);
  17.  
  18.  
  19.     function printFirstResult (array) {
  20.         array.forEach(element => {
  21.             console.log(element);
  22.         })
  23.     }
  24.  
  25.     function printSecondResult (map) { // how to print map
  26.         for (let[key,value] of map.entries()) {
  27.             console.log(`${key} = ${value}`);
  28.         }
  29.     }
  30. }
  31.  
  32.  
  33. solve('cat', 42, 50, function () { console.log('Hello world!'); })
Add Comment
Please, Sign In to add comment