Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input){
- let garages = new Map();
- function createCarObj(string){
- let car = new Map();
- let arrayfromString = string.split(', ');
- let pairs = arrayfromString.map(keyVal=>{
- return keyVal.split(': ');
- });
- pairs.forEach(pair=>{
- let key = pair[0]
- let value = pair[1];
- car.set(key,value);
- })
- return car;
- }
- for (const line of input) {
- let [garage, singleCar] = line.split(' - ');
- if(!garages.has(garage)){
- let helpArr = [];
- helpArr.push(createCarObj(singleCar))
- garages.set(garage, helpArr);
- }else{
- let currentStatus = garages.get(garage);
- currentStatus.push(createCarObj(singleCar));
- garages.set(garage,currentStatus);
- }
- }
- Array.from(garages)
- .forEach(pair=>{
- console.log(`Garage № ${pair[0]}`);
- let currentGarageCars = [];
- currentGarageCars.push(...pair[1]);
- currentGarageCars.forEach(map=>{
- let output = [];
- Array.from(map).forEach(pair=>{
- output.push(`${pair[0]} - ${pair[1]}`);
- })
- console.log(`--- ${output.join(', ')}`);
- })
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment