Advertisement
Prohause

Travel time

Jun 10th, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. function time(input) {
  2. let allCountry = {};
  3.  
  4. for (const string of input) {
  5. let [country, town, cost] = string.split(' > ');
  6. cost = Number(cost);
  7. town = town[0].toLocaleUpperCase() + town.substr(1);
  8.  
  9. if (!allCountry.hasOwnProperty(country)) {
  10. allCountry[country] = {};
  11. }
  12. if (!allCountry[country].hasOwnProperty(town)) {
  13. allCountry[country][town] = cost;
  14. }
  15. if (cost < allCountry[country][town]) {
  16. allCountry[country][town] = cost;
  17. }
  18.  
  19. }
  20.  
  21. let result = Object.keys(allCountry).sort();
  22. for (const string of result) {
  23. let output = `${string} ->`;
  24. let inner = Object.keys(allCountry[string]).sort(function (x,y) {
  25. return allCountry[string][x]-allCountry[string][y];
  26. });
  27. for (const string1 of inner) {
  28. output+=` ${string1} -> ${allCountry[string][string1]}`;
  29. }
  30. console.log(output);
  31. }
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement