Advertisement
bebo231312312321

Untitled

Mar 9th, 2023
41
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.         }
  19.  
  20.     });
  21.     let coutrySorrted = Object.entries(destination).sort(sortedCountry)
  22.     for (let [name, town] of coutrySorrted){
  23.         let townEntries = Object.entries(town).sort(sortedTownPrice)
  24.         let output = townEntries.map((keys )=>`${keys[0]} -> ${keys[1]}`).join(" ")
  25.         let print = `${name} -> ${output}`
  26.         console.log(print)
  27.     }
  28.     function  sortedCountry  (firstCountry,secondCountry){
  29.         return firstCountry[0].localeCompare(secondCountry[0])
  30.     }
  31.     function sortedTownPrice(firstPrice, secondPrice){
  32.     return firstPrice[1] - secondPrice[1]
  33.    }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement