Below is my code.
Sample values for oddCells function are n = 2, m = 3, indices = [[0,1],[1,1]].
My incrementColumn helper function works fine. The forEach loop works on the inside but then the incremented values are not saved back into arr[row] for some reason.
Question is from LeetCode "Cells with Odd Values in a Matrix"
var incrementRow = (row, arr) => {
arr[row].forEach((e) => e++);
}
var incrementColumn = (col, arr) => {
arr.forEach((e) => e[col]++);
}
var oddCells = (n, m, indices) => {
//Define our matrix
let matrix = [];
//Initialize matrix with zeroes
for(let i = 0; i < n; i++) {
matrix[i] = []
for(let j = 0; j < m; j++) {
matrix[i][j] = 0;
}
}
console.log(matrix);
//Iterate through indices array
for(let i = 0; i < indices.length; i++) {
incrementRow(indices[i][0], matrix);
incrementColumn(indices[i][1], matrix);
}
console.log(matrix);
};