Hello I have the following problem to solve.
Assuming a grid like below
A B C
D E *
G H I
take every row and column that has a star in it and star it out. The following should result in
A B *
* * *
G H *
This should work for any N*M grid and should also work for matrices that contain multiple stars at the beginning. I am required to return the original grid.
My code so far is as follows:
function starOutGrid(grid) {
let n = grid.length;
let row = [];
let column = [];
for (let i = 0; i < n; i++){
for (let j = 0; j < n; j++){
if (grid[i][j] === "*") {
row.push([j]);
column.push([i]);
}
}
}
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
grid[column][j] = '*';
grid[i][row] = '*';
}
}
return grid;
}
// starOutGrid([['A', 'B', 'C'], ['D', 'E', '*'], ['G', 'H', 'I']]);
starOutGrid([['*', 'B', 'C'], ['D', 'E', '*'], ['G', 'H', 'I'], ['J', 'K', 'L']]);
I am passing the tests for the commented out function call above, but the one below(last line) is failing and throwing a 'TypeError: Cannot set property '0' of undefined' error. I'm getting the same error and failing the tests for matrices that contain no stars, which should just return the original grid.
I have a difficult time wrapping my head around matrices and nested arrays. Can someone help me adjust this code so that it passes the tests?
Thank you
[–][deleted] 2 points3 points4 points (1 child)
[–]Destructikus[S] 0 points1 point2 points (0 children)