Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solveCurrentProblem(input){
- let destinationInformator = {};
- for(let destinationInfo of input){
- let[country, town, cost] = destinationInfo.split(" > ");
- let upperLetter = town[0].toUpperCase();
- town = town.replace(town[0], upperLetter);
- if(destinationInformator.hasOwnProperty(country) === false){
- destinationInformator[country] = {};
- }
- if(destinationInformator[country].hasOwnProperty(town) === false){
- destinationInformator[country][town] = Number(cost);
- } else {
- let currentCost = destinationInformator[country][town];
- if(cost < currentCost){
- destinationInformator[country][town] = Number(cost);
- }
- }
- }
- let sortedCountries = Object.keys(destinationInformator).sort((a,b) => a.localeCompare(b));
- let outputFormat = "";
- for(let country of sortedCountries){
- outputFormat += country + " -> ";
- let sortedTowns = Object.keys(destinationInformator[country]).sort((a,b) => {
- let difference = destinationInformator[country][a] - destinationInformator[country][b];
- return difference;
- });
- let townAndPrice = [];
- for(let town of sortedTowns){
- townAndPrice.push(town + " -> " + destinationInformator[country][town]);
- }
- outputFormat += townAndPrice.join(" ") + "\n";
- }
- console.log(outputFormat);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement