Codehs 8.1.5 Manipulating 2d Arrays Link
function rotateClockwise(matrix) let result = []; let rows = matrix.length; let cols = matrix[0].length;
for (let j = 0; j < cols; j++) let newRow = []; for (let i = rows - 1; i >= 0; i--) newRow.push(matrix[i][j]); result.push(newRow); return result;
Understanding 8.1.5 isn't just about passing CodeHS—it's a foundational skill for: Codehs 8.1.5 Manipulating 2d Arrays
To remove a row from a 2D array, you can use the splice() method.
var array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
array.splice(1, 1); // remove row at index 1
console.log(array);
// output: [[1, 2, 3], [7, 8, 9]]
To remove a column from a 2D array, you need to iterate through each row and remove the corresponding element. function rotateClockwise(matrix) let result = []; let rows
var array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
for (var i = 0; i < array.length; i++)
array[i].splice(1, 1); // remove column at index 1
console.log(array);
// output: [[1, 3], [4, 6], [7, 9]]
Modifying an element in a 2D array is similar to accessing an element. You simply assign a new value to the element using its row and column index.
arrayName[rowIndex][columnIndex] = newValue;
For example:
var myArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
myArray[1][2] = 10; // myArray = [[1, 2, 3], [4, 5, 10], [7, 8, 9]];
A common mistake is using array.length for the inner loop condition.