Advertisement
kStoikow

Garage

Oct 5th, 2019
561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let garages = {};
  3.  
  4.     for (const line in input) {
  5.         let [garage, carInfo] = input[line].split(' - ');
  6.  
  7.         if (!garages.hasOwnProperty(garage)) {
  8.             garages[garage] = {};
  9.         }
  10.  
  11.         garages[garage][line] = carInfo;
  12.     }
  13.  
  14.     let entries = Object.entries(garages);
  15.  
  16.     for (const [garage, carObj] of entries) {
  17.         console.log(`Garage № ${garage}`);
  18.  
  19.         for (const key in carObj) {
  20.             while (carObj[key].includes(': ')) {
  21.                 carObj[key] = carObj[key].replace(': ', ' - ');
  22.             }
  23.  
  24.             console.log(`--- ${carObj[key]}`);
  25.         }
  26.     }
  27. }
  28. solve(['1 - color: blue, fuel type: diesel',
  29.     '1 - color: red, manufacture: Audi',
  30.     '2 - fuel type: petrol',
  31.     '4 - color: dark blue, fuel type: diesel, manufacture: Fiat'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement