Advertisement
Liliana797979

auto-engineering company - js advanced

Oct 8th, 2021
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.  
  3.     const carBrands = new Map();
  4.  
  5.     input.forEach((el)=> {
  6.         let [brand, model, count] = el.split(" | ");
  7.         count = Number(count);
  8.  
  9.         if (carBrands.has(brand)) {
  10.             let carBrand = carBrands.get(brand);
  11.             if (carBrand.has(model)) {
  12.                 let carModel = carBrand.get(model);
  13.                 carModel += count;
  14.                 carBrand.set(model, carModel);
  15.             } else {
  16.                 carBrand.set(model, count);
  17.             }
  18.         } else {
  19.             const modelMap = new Map();
  20.             modelMap.set(model, count);
  21.             carBrands.set(brand, modelMap);
  22.  
  23.         }
  24.     })
  25.  
  26.     for (const key of carBrands.keys()) {
  27.         console.log(key);
  28.         const brand = carBrands.get(key);
  29.         for (const [model, count] of brand) {
  30.  
  31.             console.log(`###${model} -> ${count}`);
  32.         }
  33.     }
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement