Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function travelTime(input) {
- let countries = {};
- input.forEach(element => {
- let [country, town, currentPrice] = element.split(" > ");
- currentPrice = Number(currentPrice);
- if (!(countries.hasOwnProperty(country)) {
- countries[country] = {};
- countries[country][town] = currentPrice;
- } else {
- let countryObj = countries[country];
- if (!countryObj.hasOwnProperty(town)) {
- countryObj[town] = currentPrice;
- } else {
- let oldPrice = countryObj[town];
- if (oldPrice > currentPrice) {
- countryObj[town] = currentPrice;
- }
- }
- }
- });
- let sortCountries = Object.entries(countries)
- .sort(sortCountries);
- for (let [name, towns] of sortCountries) {
- let sortTowns = Object.entries(towns).sort(sortTowns);
- let output = ` ${name} ->` ;
- for (const [townName, townPrice] of sortTowns) {
- output += `${townName} -> ${townPrice}`;
- }
- console.log(output);
- }
- function sortCountries(firstCountry, secondCountry) {
- let firstName = firstCountry[0];
- let secondName = secondCountry[0];
- return firstName.localeCompare(secondCountry);
- }
- function sortTowns(firstTown, secondTown) {
- let firstPrice = firstTown[1];
- let secondPrice = secondTown[1];
- return firstPrice - secondPrice;
- }
- travelTime([
- "Bulgaria > Sofia > 500",
- "Bulgaria > Sopot > 800",
- "France > Paris > 2000",
- "Albania > Tirana > 1000",
- "Bulgaria > Sofia > 200"
- ]);
Advertisement
Add Comment
Please, Sign In to add comment