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. while(input.length>0){
  4.     [country, town, cost] = input.shift().split(" > ")
  5.     cost=Number(cost)
  6.  
  7.     if(destination.hasOwnProperty(country)){
  8.         if(destination[country].hasOwnProperty(town)){
  9.             let price = destination[country][town]
  10.             if(price>cost){
  11.                 destination[country][town]=cost
  12.             }
  13.  
  14.         }else {
  15.             destination[country][town]=cost
  16.         }
  17.     }else{
  18.         destination[country] = {
  19.             [town]:cost
  20.         }
  21.     }
  22.      
  23. }
  24. for(let country of Object.keys(destination).sort((a,b)=>a.localeCompare(b))){
  25.    
  26.     let forPrint = country + " -> "
  27.     let entries = Object.entries(destination[country]).sort(([a,valA],[b,valB])=> valA - valB)
  28.     for(let [key, valuea] of entries){
  29.         forPrint+= key+ " -> "+ valuea + " "
  30.     }
  31.     console.log(forPrint)
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement