MCPcopy Create free account
hub / github.com/btholt/algorithms-exercises / getNeighbors

Function getNeighbors

specs/pathfinding/pathfinding.solution.test.js:77–101  ·  view source on GitHub ↗
(visited, x, y)

Source from the content-addressed store, hash-verified

75};
76
77const getNeighbors = (visited, x, y) => {
78 const neighbors = [];
79
80 if (y - 1 >= 0 && !visited[y - 1][x].closed) {
81 // left
82 neighbors.push(visited[y - 1][x]);
83 }
84
85 if (y + 1 < visited[0].length && !visited[y + 1][x].closed) {
86 // right
87 neighbors.push(visited[y + 1][x]);
88 }
89
90 if (x - 1 >= 0 && !visited[y][x - 1].closed) {
91 // up
92 neighbors.push(visited[y][x - 1]);
93 }
94
95 if (x + 1 < visited.length && !visited[y][x + 1].closed) {
96 // down
97 neighbors.push(visited[y][x + 1]);
98 }
99
100 return neighbors;
101};
102
103// unit tests
104// do not modify the below code

Callers 1

findShortestPathLengthFunction · 0.85

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected