Skip to content

Instantly share code, notes, and snippets.

@llaryssa
Created January 4, 2022 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save llaryssa/79564e383f4e62b9d386112c7951d1db to your computer and use it in GitHub Desktop.
Save llaryssa/79564e383f4e62b9d386112c7951d1db to your computer and use it in GitHub Desktop.
const pathBetweenPoints = (list, start, end, currPath = []) => {
const node = list.find(({ name }) => name === start);
currPath = [...currPath, start];
if (node.connections.some((element) => element === end)) {
console.log([...currPath, end].join(" -> "));
return; // Hopefully this will break in the shortest path
} else {
node.connections?.forEach((name) => {
if (!currPath.find((element) => element === name)) {
pathBetweenPoints(list, name, end, currPath);
}
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment