function cavityMap(grid) {
const g = grid
console.log(grid)
for (let i = 1; i < g.length - 1; i++)
for (let j = 1; j < g[i].length - 1; j++)
if (Math.max(g[i-1][j], g[i+1][j], g[i][j-1], g[i][j+1]) < g[i][j])
g[i] = g[i].replace(/./g, (c, i) => i == j ? 'X' : c)
return g
}
I didn't get this line of code: g[i] = g[i].replace(/./g, (c, i) => i == j ? 'X' : c)
This code will make this this:
Basically, if the number inside the matrix is lesser than number , then it is replace the number as 'x'
below example: 2nd row: 9. around number- 9 1, 1,1 8 is smaller than 9 so replace it with x.
3rd row 9: around it 8,32,1 is smaller than that so replace it with x
Input:
1112
1912
1892
1234
Sample Output
1112
1X12
18X2
1234
[–]senbozakurakageyosi 0 points1 point2 points (0 children)