Created
December 17, 2019 08:35
-
-
Save vinicius5581/ee5267c0358e703805dbf1e921750c84 to your computer and use it in GitHub Desktop.
Find matrix point neighbors
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 getNeighboors = (matrix, row,col) => { | |
let isTopBorder = row === 0; | |
let isRightBorder = col === matrix[row].length - 1; | |
let isBottomBorder = row === matrix.length - 1; | |
let isLeftBorder = col === 0; | |
let top = isTopBorder ? row : row - 1; | |
let left = isLeftBorder ? col : col - 1; | |
let bottom = isBottomBorder ? row : row + 1; | |
let right = isRightBorder ? col : col + 1; | |
const neighboors = []; | |
for(let r = top; r <= bottom; r++) { | |
for(let c = left; c <= right; c++){ | |
if(!(row === r && col === c)) { | |
neighboors.push(matrix[r][c]); | |
} | |
} | |
} | |
console.log(neighboors); | |
} | |
const matrixTest = [ | |
[1,2,3,4,5,6], | |
[4,5,6,7,8,9], | |
[7,8,9,3,4,2], | |
]; | |
getNeighboors(matrixTest, 0, 5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment