Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. function solve(input) {
  2. let obj = {};
  3.  
  4. for (let travelInfo of input) {
  5. let [country, town, cost] = travelInfo.split(" > ");
  6.  
  7. if (!obj.hasOwnProperty(country)) {
  8. obj[country] = [];
  9. }
  10. let currentTown = obj[country].find((o) => o.town === town);
  11. if (!currentTown) {
  12. obj[country].push({town, cost: (+cost)})
  13. } else if ((+cost) < currentTown.cost) {
  14. currentTown.cost = (+cost);
  15. }
  16. }
  17.  
  18. let output = "";
  19. Object.keys(obj).sort((a,b) => a.toLowerCase().localeCompare(b.toLowerCase())).forEach((country) => {
  20. output += `${country} -> `;
  21. obj[country].sort((a, b) => a.cost - b.cost).forEach((town) => {
  22. output += `${town.town} -> ${town.cost} `
  23. });
  24. output += '\n'
  25. });
  26.  
  27. console.log(output)
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement