Advertisement
bebo231312312321

Untitled

Mar 9th, 2023
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function travelTime(input) {
  2.     let destination = {}
  3.     input.forEach(line => {
  4.         [country, town, price] = line.split(" > ")
  5.         price = Number(price)
  6.         if (!destination.hasOwnProperty(country)) {
  7.             destination[country] = {}
  8.             destination[country][town] = price
  9.         } else {
  10.             if (!destination[country].hasOwnProperty(town)) {
  11.                 destination[country][town] = price
  12.             } else {
  13.                 let oldPrice = destination[country][town]
  14.                 if (oldPrice > price) destination[country][town] = price      
  15.             }
  16.         }
  17.     });
  18.     let coutrySorrted = Object.entries(destination).sort(sortedCountry)
  19.     for (let [name, town] of coutrySorrted){
  20.         let townEntries = Object.entries(town).sort(sortedTownPrice)
  21.         let output = townEntries.map((keys )=>`${keys[0]} -> ${keys[1]}`).join(" ")
  22.         let print = `${name} -> ${output}`
  23.         console.log(print)
  24.     }
  25.     function  sortedCountry  (firstCountry,secondCountry){
  26.         return firstCountry[0].localeCompare(secondCountry[0])
  27.     }
  28.     function sortedTownPrice(firstPrice, secondPrice){
  29.     return firstPrice[1] - secondPrice[1]
  30.    }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement