MCPcopy Create free account
hub / github.com/marijnh/Eloquent-JavaScript / findPath

Function findPath

code/solutions/22_1_pathfinding.js:1–12  ·  view source on GitHub ↗
(a, b)

Source from the content-addressed store, hash-verified

1function findPath(a, b) {
2 let work = [[a]];
3 for (let path of work) {
4 let end = path[path.length - 1];
5 if (end == b) return path;
6 for (let next of end.edges) {
7 if (!work.some(path => path[path.length - 1] == next)) {
8 work.push(path.concat([next]));
9 }
10 }
11 }
12}
13
14let graph = treeGraph(4, 4);
15let root = graph[0], leaf = graph[graph.length - 1];

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected