Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- const destinations = {};
- const travelCost = (town1, town2, destination, country) => {
- const priceOne = destination[country][town1];
- const priceTwo = destination[country][town2];
- return priceOne - priceTwo;
- }
- input.forEach((element) => {
- let [country, town, price] = element.split(' > ').filter((e) => e !== '');
- town = town[0].toUpperCase() + town.slice(1);
- if (!destinations.hasOwnProperty(country)) {
- destinations[country] = {};
- }
- if (!destinations[country].hasOwnProperty(town)) {
- destinations[country][town] = Number(price);
- }
- const prevPrice = destinations[country][town];
- if (prevPrice > price) {
- destinations[country][town] = price;
- }
- });
- const orderedCountries = [...Object.keys(destinations)].sort((a, b) => a.localeCompare(b));
- let result = '';
- orderedCountries.forEach((country) => {
- result += `${country} -> `;
- const sortedPrices = [...Object.keys(destinations[country])].sort((a, b) => travelCost(a, b, destinations, country));
- sortedPrices.forEach((town) => {
- result += `${town} -> ${destinations[country][town]} `;
- });
- result += '\n';
- });
- console.log(result);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement