console.log(array); // output: [[1, 2, 3, 10], [4, 5, 6, 10], [7, 8, 9, 10]]
var myArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; Codehs 8.1.5 Manipulating 2d Arrays
// Adding a column for (var i = 0; i < array.length; i++) array[i].push(0); console
console.log(grid[0][2]); // 3 (row 0, col 2) // output: [[1
// Sum of a specific column int colSum = 0; for (int row = 0; row < matrix.length; row++) colSum += matrix[row][colIndex];
| Mistake | Solution | |---------|----------| | Using matrix.length for columns | Use matrix[0].length for columns (if rectangular) | | Forgetting rows can have different lengths (jagged arrays) | Always check matrix[i].length in inner loop | | Modifying original array when you shouldn't | Copy the array first: let copy = matrix.map(row => [...row]); | | Off-by-one errors in loops | Use < matrix.length , not <= | | Trying to access index out of bounds | Ensure row and col are valid before using |
console.log(array); // output: [[1, 2, 3, 10], [4, 5, 6, 10], [7, 8, 9, 10]]
var myArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
// Adding a column for (var i = 0; i < array.length; i++) array[i].push(0);
console.log(grid[0][2]); // 3 (row 0, col 2)
// Sum of a specific column int colSum = 0; for (int row = 0; row < matrix.length; row++) colSum += matrix[row][colIndex];
| Mistake | Solution | |---------|----------| | Using matrix.length for columns | Use matrix[0].length for columns (if rectangular) | | Forgetting rows can have different lengths (jagged arrays) | Always check matrix[i].length in inner loop | | Modifying original array when you shouldn't | Copy the array first: let copy = matrix.map(row => [...row]); | | Off-by-one errors in loops | Use < matrix.length , not <= | | Trying to access index out of bounds | Ensure row and col are valid before using |