JavaScript: Building an itinerary

Last update: 11 Dec 23:20
0
Students

itinerary.js

Implement and export as default a function that builds an itinerary between cities.

The function takes 3 arguments:

  • tree of cities
  • starting city
  • destination city

and returns an array of cities arranged in the same order in which they are located on the itinerary.

Examples

const tree = ['Chicago', [
  ['Milwaukee'],
  ['Rockford'],
  ['Indianapolis', [
    ['Fort Wayne'],
    ['Louisville'],
    ['St. Louis', [
      ['Kansas City', [
        ['Wichita'],
      ]],
      ['Memphis'],
    ]],
  ]],
  ['Columbus', [
    ['Cleveland'], ['Pittsburgh'],
  ]],
  ['Des Moines'],
  ['Minneapolis', [
    ['Sioux Falls'], ['Duluth'], ['Fargo'],
  ]],
]];

itinerary(tree, 'Chicago', 'Duluth');
// [ 'Chicago', 'Minneapolis', 'Duluth' ]

itinerary(tree, 'Fargo', 'Rockford');
// [ 'Fargo', 'Minneapolis', 'Chicago', 'Rockford' ]

Tips

  • Use the lodash lib functions

For full access to the challenge you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests