A solution to the Journey Planner programming challenge

Here is my Javascript solution to the following problem:

Given an un-ordered list of flights taken by someone compute the person's itinerary, given a starting airport.

If none valid itinerary, return none. If more than one journey is valid, print any valid solution.

Examples

Array: [['SFO', 'HKO'], ['YYZ', 'SFO'], ['YUL', 'YYZ'], ['HKO', 'ORD']]
Starting: 'YUL'
Return: ['YUL', 'YYZ', 'SFO', 'HKO', 'ORD'].

Array: [['SFO', 'COM'], ['COM', 'YYZ']]
Starting airport 'COM'
Return None

Array: [['A', 'B'], ['A', 'C'], ['B', 'C'], ['C', 'A']]
Starting: 'A'
Return ['A', 'B', 'C', 'A', 'C']


Thank you

function plan_journey(place, journeys, route, itinerary){
if(journeys.length == 0){
itinerary.push(route + place)
}
let stops = []
for(i=0; i<journeys.length; i++){
if(journeys[i][0] == place){
stops.push(i)
}
}
if(stops == []){
return
}

for(var i=0; i<stops.length; i++){
let slice_index = stops[i]
let remaining_journeys = journeys.slice(0,slice_index).concat(journeys.slice(slice_index + 1))
let new_route = route + place + "-"
let next_place = journeys[stops[i]][1]
plan_journey(next_place, remaining_journeys, new_route, itinerary)
}
return itinerary
}

let first_stop = "A"
let journeys = [['A', 'B'], ['A', 'C'], ['B', 'C'], ['C', 'A']]
itinerary = plan_journey(first_stop, journeys, "", [])
if(itinerary.length != 0){
console.log(itinerary[0])
}else{
console.log("None")
}


There are different ways to implement this, mainly based on how you deal with storing the information of the journeys, routes and locations. It should be possible to implement the above algorithm in such a way that it only require $$\O(E)\$$ runtime and extra space, with $$\E\$$ being the number of journeys.