Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- collectAndOrder = function (arr) {
- let destinations = {};
- for (let row of arr) {
- let [country, city, cost] = row.split(' > ');
- if (city[0].toLowerCase() === city[0]) {
- city = city.substr(0, 1).toUpperCase() + city.substr(1);
- }
- if (!destinations.hasOwnProperty(country)) {
- destinations[country] = [];
- }
- if (!destinations[country].hasOwnProperty(city)) {
- destinations[country][city] = Number(cost);
- } else {
- if (destinations[country][city] > Number(cost)) {
- destinations[country][city] = Number(cost);
- }
- }
- }
- let sortedCountries = Object.keys(destinations).sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()));
- for (let country of sortedCountries) {
- let result = [];
- let sortedTowns = Object.keys(destinations[country]).sort((a, b) => destinations[country][a] - destinations[country][b]);
- result.push(country + ' ->');
- for (let town of sortedTowns) {
- result.push(town + ' -> ' + destinations[country][town]);
- }
- console.log(result.join(' '));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement