MCPcopy Create free account
hub / github.com/betomoedano/JavaScript-Coding-Interview-Questions / getNeighbors

Function getNeighbors

matrix/remove-islands.js:123–135  ·  view source on GitHub ↗
(matrix, row, col)

Source from the content-addressed store, hash-verified

121}
122
123function getNeighbors(matrix, row, col) {
124 const neighbors = [];
125
126 const numRows = matrix.length;
127 const numCols = matrix[row].length;
128
129 if (row - 1 >= 0) neighbors.push([row - 1, col]); //UP
130 if (row + 1 < numRows) neighbors.push([row + 1, col]); //DOWN
131 if (col - 1 >= 0) neighbors.push([row, col - 1]); //LEFT
132 if (col + 1 < numCols) neighbors.push([row, col + 1]); //RIGHT
133
134 return neighbors;
135}
136
137const input = [
138 [1, 0, 0, 0, 0, 0],

Callers 1

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected