Codehs 8.1.5 | Manipulating 2d Arrays

This guide covers the key concepts, common patterns, and step-by-step solutions you would need to understand and complete the exercises successfully.


Example Solution:

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;


Example Solution:

function reverseEachRow(matrix) 
  let result = [];
  for (let i = 0; i < matrix.length; i++) 
    let reversedRow = [];
    for (let j = matrix[i].length - 1; j >= 0; j--) 
      reversedRow.push(matrix[i][j]);
result.push(reversedRow);
return result;

"Write a function that replaces all negative numbers in a 2D array with 0." Codehs 8.1.5 Manipulating 2d Arrays

Common Mistakes & Debugging Tips

| 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 |


Summary

In CodeHS 8.1.5, you learned to:

  1. Traverse 2D arrays with nested loops.
  2. Modify elements individually or globally.
  3. Add, remove, swap, and rotate rows/columns.
  4. Apply transformations like reversing or filtering.
  5. Avoid common pitfalls like jagged arrays and off-by-one errors.

Mastering 2D array manipulation will prepare you for more advanced topics like multidimensional data processing and algorithm design. This guide covers the key concepts, common patterns,

Next up: 8.1.6 – 2D Array Challenges & Algorithms

Since "CodeHS 8.1.5" typically refers to the exercise "Manipulating 2D Arrays" (often part of the AP Computer Science A or Intro to CS curriculum in Java), this article is tailored to explain the concepts and logic needed to solve that specific challenge.


4. Forgetting to Save a Temporary Variable

In a swap, you cannot write:

matrix[r][a] = matrix[r][b];
matrix[r][b] = matrix[r][a]; // Wrong! Original value is lost.

Always use temp.

Removing Rows and Columns

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. i++) for (let j = 0

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]]

Final Checklist Before Submitting


Example Solution:

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)."