Code style
Use constants for variables that do not change. Eg const sortedArray = [];
Don't include the type in the name, Eg const sortedArray = [];
can be Eg const sorted = [];
The default parameter in this case seams inappropriate as it is likely that "JFK"
is not an origin in all calls to this function. If no origin is given one could assume that the first item in trips contains the origin. function sortByLinked(trips, origin = trips[0].from) {
which will throw an error if trips is empty so the function should not be called with an empty trips array if you don't pass the from
parameter.
However in this example best to leave the default as undefined
if not passed as that will return an empty array which is more fitting the input parameters.
The name sort
is inappropriate as in JS it implies that the array be sorted in place, that all items be sorted (may not be possible).
You have declared i
two times. As a var
you should put the declaration at the top of the function and not in the for
loop.
Rather than use Array.filter
you can use Array.find
. It will find the first instance.
Using for...of
rather than for(;;)
reduces the code complexity.
No point continuing the search inside the for loops when you have found a match. Use the break
token to stop a loop early
Put a space between if
and (
Don't forget to add the ;
where appropriate. It is missing from the call sortByLinked(trips)
You call the origin origin
and from
this can get confusing. Keep the naming unambiguous. As the trip items use from
then that would be the better name for the second input argument.
Using the above points to modify your code we get
function tripFrom(trips, from) {
const sorted = [];
const first = trips.find(trip => trip.from === from);
sorted.push(first);
for (const trip of trips) {
if (first.to === trip.from) {
sorted.push(trip);
break;
}
}
for (const trip of trips) {
if (sorted[sorted.length - 1].to === trip.from) {
sorted.push(trips);
break;
}
}
return sorted;
}
sortByLinked(trips, "JFK");
This is still not a good solution. Its not at all DRY (don't repeat yourself) and is hard coded to a single use case.
Improving the function.
It can all be done within a single loop and work for any length array.
To create the function we must add some constraints on the array trips
and what to do when we encounter any problems.
- That the array
trips
contains objects that each have the property from
and to
that are correctly formatted static strings. The resulting array is erroneous or indeterminate if not so.
- That the array does not contain circular trips shorter than the array length.
- That a complete trip length is no longer than the array, or when a matching
trip.to
can not be found. The returned array can be 0 to trips.length
in size.
- Locations are case sensitive.
- If there is more than one matching
trip.from
it is assumed that the first match in trips is the correct one. (It would be interesting to extract the longest possible trip from? or the shortest trip that returns to the origin?)
Example
function tripFrom(trips, from) {
const result = [];
while (result.length < trips.length) {
const trip = trips.find(trip => trip.from === from);
if (!trip) { break }
from = trip.to;
result.push(trip);
}
return result;
}
tripFrom(trips, "JFK");
Or if it is known that the trip is the same length as the input array.
function tripFrom(trips, from) {
const res = [];
while (res.length < trips.length) {
from = (res[res.length] = trips.find(trip => trip.from === from)).to;
}
return res;
}
tripFrom(trips, "JFK");
It is unclear if you want the array sorted in place. If that is a requirement then the above version can be modified to do that by simply copying the results array res
to the trips
array. You can empty an array by setting its length to zero. The spread ...
operator in this case spreads the array items over the functions arguments trips.push(...res)
thus pushing all the items to the array.
function tripFrom(trips, from) {
const res = [];
while (res.length < trips.length) {
from = (res[res.length] = trips.find(trip => trip.from === from)).to;
}
trips.length = 0;
trips.push(...res);
return trips;
}
tripFrom(trips, "JFK");
from
unique for all elements? \$\endgroup\$