Advertisement
bebo231312312321

Untitled

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