Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function travelTime(input) {
- let travels = {}
- input.forEach(row => {
- let [country, city, miles] = row.split(' > ')
- if (!travels.hasOwnProperty(country)) {
- travels[country] = {}
- }
- if (!travels[country].hasOwnProperty(city)) {
- travels[country][city] = Number.POSITIVE_INFINITY;
- }
- if (travels[country].hasOwnProperty(city)) {
- if (travels[country][city] > Number(miles)) {
- travels[country][city] = Number(miles)
- }
- }
- })
- Object.entries(travels).sort((a, b) => a[0].localeCompare(b[0]))
- .map(row => {
- let rowOne = Object.entries(row[1]).sort((a, b) => a[1] - b[1]).map(x => x.join(' -> '))
- console.log(`${row[0]} -> ${rowOne.join(' ')} `)
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement