How to useNested For Loops in Javascript

  • In this approach, we use the nested for loops where we are iterating over the diagonals of the matrix by alternating between upside and downside directions.
  • We use the nested loops to go through the diagonals by adjusting the positions to ensure that the correct traversal is done in both row and column and then we print the elements in the diagonal pattern.

Example: In this example, we print Matrix in Diagonal Pattern in JavaScript using Nested For Loops.

Javascript




// Input matrix
let mat = [
    [1, 2, 3, 10],
    [4, 5, 6, 11],
    [7, 8, 9, 12],
    [13, 14, 15, 16]
];
 
// Getting the number of rows
// and columns in the matrix
let rowLen = mat.length;
let colLen = mat[0].length;
 
// Looping through diagonals
for (let i = 0; i < rowLen + colLen - 1; i++) {
 
    // Checking if current
    // diagonal is even or odd
    if (i % 2 === 0) {
     
        // Looping through elements in
        // the current diagonal (even)
        for (let j = Math.min(i, rowLen - 1); j >= 0
            && i - j < colLen; j--) {
             
            // Print the element in
            // the current diagonal
            console.log(mat[j][i - j]);
        }
    } else {
     
        // Looping through elements in
        // the current diagonal (odd)
        for (let j = Math.min(i, colLen - 1); j >= 0
            && i - j < rowLen; j--) {
             
            // Printing the element
            // in the current diagonal
            console.log(mat[i - j][j]);
        }
    }
}


Output

1
2
4
7
5
3
10
6
8
13
14
9
11
12
15
16

JavaScript Program to Print Matrix in Diagonal Pattern

We have given the n*n size matrix and we need to print the elements of this matrix in the diagonal pattern using JavaScript language. Below are the examples for a better understanding of the problem statement.

Examples:

Input: mat[3][3] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
Output:
1 2 4 7 5 3 6 8 9
Explanation:
Start from 1
Then from upward to downward diagonally i.e. 2 and 4
Then from downward to upward diagonally i.e 7, 5, 3
Then from up to down diagonally i.e 6, 8
Then down to up i.e. end at 9.

Input: mat[4][4] = {{1, 2, 3, 10},
{4, 5, 6, 11},
{7, 8, 9, 12},
{13, 14, 15, 16}}
Output: 1 2 4 7 5 3 10 6 8 13 14 9 11 12 15 16
Explanation : Start from 1 Then from upward to downward diagonally i.e. 2 and 4
Then from downward to upward diagonally i.e 7, 5, 3
Then from upward to downward diagonally i.e. 10 6 8 13
Then from downward to upward diagonally i.e 14 9 11
Then from upward to downward diagonally i.e. 12 15 then end at 16 .

These are the following approaches to solve this problem:

Table of Content

  • Using Nested For Loops
  • Using ZigZag Iteration

Similar Reads

Approach 1: Using Nested For Loops

In this approach, we use the nested for loops where we are iterating over the diagonals of the matrix by alternating between upside and downside directions. We use the nested loops to go through the diagonals by adjusting the positions to ensure that the correct traversal is done in both row and column and then we print the elements in the diagonal pattern....

Approach 2: Using ZigZag Iteration

...

Contact Us