Advertisement
nikolayneykov

Untitled

Mar 19th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function printDestinations(params) {
  2.     let countries = {};
  3.  
  4.     for (let param of params) {
  5.         let tokens = param.split(' > ');
  6.         let country = tokens[0];
  7.         let town = tokens[1];
  8.         let price = Number(tokens[2]);
  9.  
  10.         if (!countries.hasOwnProperty(country)) {
  11.             countries[country] = {};
  12.         }
  13.         if (!countries[country].hasOwnProperty(town)) {
  14.             countries[country][town] = price;
  15.         }
  16.         if (countries[country][town] > price) {
  17.             countries[country][town] = price;
  18.         }
  19.     }
  20.  
  21.     let sortedCountries = Object.entries(countries)
  22.         .sort((a, b) => a[0].localeCompare(b[0]));
  23.  
  24.     for (let [country, town] of sortedCountries) {
  25.         let sortedTowns = Object.entries(town)
  26.             .sort((a, b) => a[1] - b[1])
  27.             .map(t=>t.join(' -> '));
  28.         console.log(`${country} -> ${sortedTowns.join(' ')}`);
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement