maymunskoa

01. Garage (Assoc Array /// !!! sort() removed)

Mar 8th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.  
  3. let garages = new Map();
  4.  
  5. function createCarObj(string){
  6.     let car = new Map();
  7.     let arrayfromString = string.split(', ');
  8.     let pairs = arrayfromString.map(keyVal=>{
  9.         return keyVal.split(': ');
  10.     });
  11.     pairs.forEach(pair=>{
  12.         let key = pair[0]
  13.         let value = pair[1];
  14.         car.set(key,value);        
  15.     })
  16.    
  17.     return car;
  18. }
  19.  
  20. for (const line of input) {
  21.         let [garage, singleCar] = line.split(' - ');
  22.         if(!garages.has(garage)){
  23.             let helpArr = [];
  24.             helpArr.push(createCarObj(singleCar))
  25.             garages.set(garage, helpArr);
  26.         }else{
  27.             let currentStatus = garages.get(garage);
  28.             currentStatus.push(createCarObj(singleCar));
  29.             garages.set(garage,currentStatus);
  30.         }
  31. }
  32.  
  33. Array.from(garages)
  34. .forEach(pair=>{
  35.     console.log(`Garage № ${pair[0]}`);
  36.     let currentGarageCars = [];
  37.     currentGarageCars.push(...pair[1]);
  38.     currentGarageCars.forEach(map=>{
  39.         let output = [];
  40.         Array.from(map).forEach(pair=>{
  41.             output.push(`${pair[0]} - ${pair[1]}`);
  42.         })
  43.         console.log(`--- ${output.join(', ')}`);
  44.     })
  45.    
  46. });
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment