Created
March 23, 2018 14:32
-
-
Save db001/2ac227221749bd3f516ed623f95bfde4 to your computer and use it in GitHub Desktop.
My solution to https://www.hackerrank.com/challenges/cavity-map/problem
This file contains hidden or 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
function cavityMap(grid) { | |
// Turn grid into a 2D array | |
const twoDArr = grid.map(ele => ele.split('')); | |
// Loop through 2D array checking values against neighbours | |
for(let i = 1; i < twoDArr.length - 1; i++) { | |
for(let j = 1; j < twoDArr[i].length - 1; j++) { | |
if(horizontalCheck(twoDArr, i, j) && verticalCheck(twoDArr, i, j)) { | |
twoDArr[i][j] = 'X'; | |
} | |
} | |
} | |
const newGrid = twoDArr.map(ele => ele.join('')); | |
return newGrid; | |
} | |
// Check against horizontal values | |
function horizontalCheck(arr, column, row) { | |
if(arr[column][row] > arr[column][row - 1] && arr[column][row] > arr[column][row + 1]) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
// Check against vertical values | |
function verticalCheck(arr, column, row) { | |
if(arr[column][row] > arr[column - 1][row] && arr[column][row] > arr[column + 1][row]) { | |
return true; | |
} else { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment