Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function travelTime(input) {
- let destination = {}
- input.forEach(line => {
- [country, town, price] = line.split(" > ")
- price = Number(price)
- if (!destination.hasOwnProperty(country)) {
- destination[country] = {}
- destination[country][town] = price
- } else {
- if (!destination[country].hasOwnProperty(town)) {
- destination[country][town] = price
- } else {
- let oldPrice = destination[country][town]
- if (oldPrice > price) {
- destination[country][town] = price
- }
- }
- }
- });
- let coutrySorrted = Object.entries(destination).sort(sortedCountry)
- for (let [name, town] of coutrySorrted){
- let townEntries = Object.entries(town).sort(sortedTownPrice)
- let output = townEntries.map((keys )=>`${keys[0]} -> ${keys[1]}`).join(" ")
- let print = `${name} -> ${output}`
- console.log(print)
- }
- function sortedCountry (firstCountry,secondCountry){
- return firstCountry[0].localeCompare(secondCountry[0])
- }
- function sortedTownPrice(firstPrice, secondPrice){
- return firstPrice[1] - secondPrice[1]
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement