kstoyanov

05. Neighborhoods js fundamentals

Jul 11th, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(args) {
  2.   const neighborhood = {};
  3.   args.shift()
  4.     .split(', ')
  5.     .forEach((name) => neighborhood[name] = []);
  6.  
  7.   args.forEach((element) => {
  8.     const [district, name] = element.split(' - ');
  9.  
  10.     if (Object.prototype.hasOwnProperty.call(neighborhood, district)) {
  11.       neighborhood[district].push(name);
  12.     }
  13.   });
  14.  
  15.   const districtSortedByCount = Object.entries(neighborhood).sort((a, b) => b[1].length - a[1].length);
  16.  
  17.   districtSortedByCount.forEach((element) => {
  18.     const [district, inhabitants] = element;
  19.  
  20.     console.log(`${district}: ${inhabitants.length}`);
  21.     inhabitants.forEach((person) => {
  22.       console.log(`--${person}`);
  23.     });
  24.   });
  25. }
Add Comment
Please, Sign In to add comment