Codehs 8.1.5 Manipulating 2d Arrays

function zeroOutNegatives(matrix) 
  for (let i = 0; i < matrix.length; i++) 
    for (let j = 0; j < matrix[i].length; j++) 
      if (matrix[i][j] < 0) 
        matrix[i][j] = 0;
return matrix;

"Write a function that returns the sum of the border elements (first row, last row, first column, last column)."

While specific exercise details can vary slightly by course version, "Manipulating 2D Arrays" generally asks you to modify the values inside an existing grid. Common tasks include adding a constant to every number, squaring every number, or replacing specific values. Codehs 8.1.5 Manipulating 2d Arrays

Let's look at a common scenario: Adding 5 to every element in a 2D integer array. function zeroOutNegatives(matrix) for (let i = 0; i

Students often encounter "IndexOutOfBounds" errors or logic errors on this exercise. Here is how to avoid them: "Write a function that returns the sum of

for (int i = 0; i < matrix.length; i++)           // For each row
    for (int j = 0; j < matrix[0].length; j++)    // For each column in that row
        System.out.print(matrix[i][j] + " ");
System.out.println();

Swap two rows:

function swapRows(matrix, rowA, rowB) 
  let temp = matrix[rowA];
  matrix[rowA] = matrix[rowB];
  matrix[rowB] = temp;
  return matrix;