Liliana797979

map exercise - js advanced

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