AreWe

garage

Apr 2nd, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. garage = (input) => {
  2. let garages = {};
  3. input.forEach(line => {
  4. let split = line.split(' - ');
  5. let num = split.shift();
  6. let car = {};
  7. split.shift().split(', ').map(part => part.split(': ')).forEach(el => {
  8. car[el[0]] = el[1];
  9. });
  10. if(garages[num] !== undefined) {
  11. garages[num].push(car);
  12. } else {
  13. garages[num] = [ car ];
  14. }
  15. });
  16.  
  17. Object
  18. .entries(garages)
  19. .sort((a, b) => b[1].length - a[1].length)
  20. .forEach(([garage, cars]) => {
  21. console.log(`Garage № ${garage}`);
  22. cars.forEach(car => {
  23. let output = '';
  24. for(key in car) {
  25. output += `, ${key} - ${car[key]}`;
  26. }
  27. output = '---' + output.substr(1);
  28. console.log(output);
  29. });
  30. });
  31. }
Advertisement
Add Comment
Please, Sign In to add comment