Advertisement
Pijomir

Travel Time

Nov 4th, 2023
828
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function checkDestinations(input) {
  2.     let destinationsList = {};
  3.     input.forEach(el => {
  4.         let [country, town, price] = el.split(' > ');
  5.         price = Number(price);
  6.         if (!destinationsList.hasOwnProperty(country)) {
  7.             destinationsList[country] = {[town]: price};
  8.         } else {
  9.             if (destinationsList[country].hasOwnProperty(town)) {
  10.                 if (price > destinationsList[country][town]) {
  11.                     price = destinationsList[country][town];
  12.                 }
  13.             }
  14.  
  15.             destinationsList[country][town] = price;
  16.         }
  17.     });
  18.     let sortedDestinations = Object.entries(destinationsList).sort((a, b) => a[0].localeCompare(b[0]));
  19.     sortedDestinations.forEach(el => {
  20.         let [country, townAndPrice] = el;
  21.         let sortedTOwns = Object.entries(townAndPrice).sort((a, b) => a[1] - b[1]).map(a => `${a[0]} -> ${a[1]}`).join(' ');
  22.         console.log(`${country} -> ${sortedTOwns}`);
  23.     });
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement