Advertisement
kstoyanov

03. Population in Towns

Sep 20th, 2020
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.   const mapWithTowns = new Map();
  3.  
  4.   arr.forEach((el) => {
  5.     const [town, population] = el.split(/\s+<->\s+/);
  6.  
  7.     if (!mapWithTowns.has(town)) {
  8.       mapWithTowns.set(town, 0);
  9.     }
  10.  
  11.     mapWithTowns.set(town, mapWithTowns.get(town) + Number(population));
  12.   });
  13.  
  14.   mapWithTowns.forEach((value, key) => {
  15.     console.log(`${key} : ${value}`);
  16.   });
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement