Created
January 4, 2022 18:08
-
-
Save llaryssa/79564e383f4e62b9d386112c7951d1db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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