Lesson 2

Hello, advancing coder! Are you excited to dive into a new, intriguing challenge? In this lesson, we're going to explore special traversals of **matrices**. Matrices are rectangular 2D arrays where we traverse through rows and columns. We'll find our way through a matrix by climbing up and down the columns, zigzagging as we go. Sound exciting? Buckle up, then, and get ready!

Here's the task: you've been given a `2D array`

consisting of individual cells, each holding a unique integer value. Your goal is to create a function that will traverse this matrix, starting at the bottom-right cell. From there, you'll travel up to the top of the same column, then move left to the next column, and then continue downward from the top of this new column. After reaching the bottom of the column, you again move left and start moving upward. This unique traversal pattern will be performed until all the cells have been visited.

Consider this small 3x4 matrix as an example:

JavaScript`1const matrix = [ 2 [1, 2, 3, 4], 3 [5, 6, 7, 8], 4 [9, 10, 11, 12] 5];`

With the described traversal pattern, your function should return this list: `[12, 8, 4, 3, 7, 11, 10, 6, 2, 1, 5, 9]`

.

The first step towards a solution is understanding the dimensions of the matrix that we're working with. We can do this using array properties. Let's set up our function and identify the matrix size:

JavaScript`1function columnTraverse(matrix) { 2 const rows = matrix.length; 3 const cols = matrix[0].length; 4}`

Now that we're aware of the matrix dimensions, we should establish the starting point (bottom-right) and the direction of travel (upward initially). Additionally, we'll need an array to keep track of the cells we've visited in order:

JavaScript`1function columnTraverse(matrix) { 2 const rows = matrix.length; 3 const cols = matrix[0].length; 4 let direction = "up"; 5 let row = rows - 1; 6 let col = cols - 1; 7 const output = []; 8 let index = 0; 9}`

It's time to go exploring! We'll now implement a `while`

loop to traverse the matrix. This loop will continue until we have covered all the cells in the matrix. As we "visit" each cell, we'll add the value in the cell to our array.

JavaScript`1function columnTraverse(matrix) { 2 const rows = matrix.length; 3 const cols = matrix[0].length; 4 let direction = "up"; 5 let row = rows - 1; 6 let col = cols - 1; 7 const output = []; 8 9 for (let index = 0; index < rows * cols; index++) { 10 output[index] = matrix[row][col]; 11 12 if (direction === "up") { 13 if (row - 1 < 0) { 14 direction = "down"; 15 col -= 1; 16 } else { 17 row -= 1; 18 } 19 } else { 20 if (row + 1 === rows) { 21 direction = "up"; 22 col -= 1; 23 } else { 24 row += 1; 25 } 26 } 27 } 28 29 return output; 30} 31 32const matrix = [ 33 [1, 2, 3, 4], 34 [5, 6, 7, 8], 35 [9, 10, 11, 12] 36]; 37 38const result = columnTraverse(matrix); 39console.log(result.join(" ")); 40 41// Outputs: 12 8 4 3 7 11 10 6 2 1 5 9`

That's it, we've completed the function! This JavaScript function will return the `output`

array, which gives us the order of traversal through the matrix.

Let's explore one more way of traversal. We can leverage the utility of JavaScript's `for`

loop to traverse a `2D matrix`

in reverse order. This flexibility can also create a sequence that decrements.

To achieve this, we use a `for`

loop with decrementing indices.

Consider our familiar 3x4 matrix:

JavaScript`1const matrix = [ 2 [1, 2, 3, 4], 3 [5, 6, 7, 8], 4 [9, 10, 11, 12] 5];`

Using decrementing loops, the reverse traverse pattern would produce this list: `[12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]`

.

Here's how we can implement this reverse traversal:

JavaScript`1function reverseTraverse(matrix) { 2 const rows = matrix.length; 3 const cols = matrix[0].length; 4 const output = []; 5 let index = 0; 6 7 for (let row = rows - 1; row >= 0; --row) { 8 for (let col = cols - 1; col >= 0; --col) { 9 output[index++] = matrix[row][col]; 10 } 11 } 12 13 return output; 14} 15 16const matrix = [ 17 [1, 2, 3, 4], 18 [5, 6, 7, 8], 19 [9, 10, 11, 12] 20]; 21 22const result = reverseTraverse(matrix); 23console.log(result.join(" ")); 24 25// Outputs: 12 11 10 9 8 7 6 5 4 3 2 1`

In this function, we start the loop for `row`

from `rows - 1`

and run it to `0`

, decrementing it at each step. Similarly, our nested loop for `col`

starts from `cols - 1`

and goes to `0`

. This allows us to start from the bottom-right corner and traverse leftward, then upward, covering the entire matrix in reverse order.

As you gain proficiency in matrix traversal in **JavaScript**, you'll discover the many creative ways to utilize JavaScript's flexible syntax. The more you experiment with different traversal methods, the better you become! Happy practicing!

Congratulations! You have made it through a challenging task in which you maneuvered your way through complex matrix traversal patterns. The functions you've designed not only test your **JavaScript** coding skills but also your ability to visualize spatial patterns.

It's time to put your new knowledge to the test! The next step is to tackle similar challenges on your own. I encourage you to use this lesson as your guide, and don't forget to experiment with different matrix sizes and cell values. With plenty of practice, you'll soon master these traversal patterns!