Advertisement
Todorov_Stanimir

05. Auto-Engineering Company Exercise: Objects

Sep 24th, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function autoEngineeringCompany(input) {
  2.     let producedCars = {};
  3.     input.forEach(element => {
  4.         let [carBrand, carModel, producedQuantityCars] = element.split(' | ');
  5.         producedQuantityCars = Number(producedQuantityCars);
  6.         if (!producedCars[carBrand]) {
  7.             producedCars[carBrand] = {};
  8.             producedCars[carBrand][carModel] = producedQuantityCars;
  9.         } else {
  10.             !producedCars[carBrand][carModel]
  11.                 ? producedCars[carBrand][carModel] = producedQuantityCars
  12.                 : producedCars[carBrand][carModel] += producedQuantityCars
  13.         }
  14.     });
  15.     Object.entries(producedCars).forEach(carBrand => {
  16.         console.log(carBrand[0]);
  17.         Object.entries(carBrand[1]).forEach(carModel => console.log(`###${carModel[0]} -> ${carModel[1]}`))
  18.     })
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement