Advertisement
miroLLL

TravelTime

Oct 12th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solveCurrentProblem(input){
  2.  
  3.     let destinationInformator = {};
  4.  
  5.     for(let destinationInfo of input){
  6.  
  7.         let[country, town, cost] = destinationInfo.split(" > ");
  8.  
  9.         let upperLetter = town[0].toUpperCase();
  10.         town = town.replace(town[0], upperLetter);
  11.  
  12.         if(destinationInformator.hasOwnProperty(country) === false){
  13.  
  14.             destinationInformator[country] = {};
  15.         }
  16.  
  17.         if(destinationInformator[country].hasOwnProperty(town) === false){
  18.  
  19.             destinationInformator[country][town] = Number(cost);
  20.  
  21.         } else {
  22.  
  23.             let currentCost = destinationInformator[country][town];
  24.  
  25.             if(cost < currentCost){
  26.  
  27.                 destinationInformator[country][town] = Number(cost);
  28.             }
  29.         }
  30.     }
  31.  
  32.     let sortedCountries = Object.keys(destinationInformator).sort((a,b) => a.localeCompare(b));
  33.  
  34.     let outputFormat = "";
  35.  
  36.     for(let country of sortedCountries){
  37.  
  38.         outputFormat += country + " -> ";
  39.  
  40.         let sortedTowns = Object.keys(destinationInformator[country]).sort((a,b) => {
  41.             let difference = destinationInformator[country][a] - destinationInformator[country][b];
  42.             return difference;
  43.         });
  44.  
  45.         let townAndPrice = [];
  46.  
  47.         for(let town of sortedTowns){
  48.  
  49.             townAndPrice.push(town + " -> " + destinationInformator[country][town]);
  50.         }
  51.  
  52.         outputFormat += townAndPrice.join(" ") + "\n";
  53.     }
  54.  
  55.     console.log(outputFormat);
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement