Advertisement
dilyana2001

Untitled

Jul 20th, 2021
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function travelTime(input) {
  2.     let travels = {}
  3.     input.forEach(row => {
  4.         let [country, city, miles] = row.split(' > ')
  5.         if (!travels.hasOwnProperty(country)) {
  6.             travels[country] = {}
  7.         }
  8.         if (!travels[country].hasOwnProperty(city)) {
  9.             travels[country][city] = Number.POSITIVE_INFINITY;
  10.         }
  11.         if (travels[country].hasOwnProperty(city)) {
  12.             if (travels[country][city] > Number(miles)) {
  13.                 travels[country][city] = Number(miles)
  14.             }
  15.         }
  16.  
  17.     })
  18.     Object.entries(travels).sort((a, b) => a[0].localeCompare(b[0]))
  19.         .map(row => {
  20.             let rowOne = Object.entries(row[1]).sort((a, b) => a[1] - b[1]).map(x => x.join(' -> '))
  21.             console.log(`${row[0]} -> ${rowOne.join(' ')} `)
  22.         })
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement