Advertisement
kstoyanov

05. Auto-Engineering Company

Sep 21st, 2020
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function autoEngineeringCompany(input) {
  2.   const cars = new Map();
  3.  
  4.   input.forEach((line) => {
  5.     const tokens = line.split(' | ');
  6.     const [brand, model, count] = tokens;
  7.  
  8.     if (!cars.get(brand)) {
  9.       cars.set(brand, new Map());
  10.     }
  11.     if (!cars.get(brand).get(model)) {
  12.       cars.get(brand).set(model, 0);
  13.     }
  14.  
  15.     cars.get(brand).set(model, cars.get(brand).get(model) + Number(count));
  16.   });
  17.  
  18.   for (const [brand, modelCount] of cars) {
  19.     console.log(brand);
  20.  
  21.     for (const [model, count] of modelCount) {
  22.       console.log(`###${model} -> ${count}`);
  23.     }
  24.   }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement