DEV Community

Abhishek Gupta
Abhishek Gupta

Posted on

DSA Pattern Printing Question with js

Question 1

Write a program using nested loops to print a 4 × 4 grid of 1s.

Expected Output

1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
Enter fullscreen mode Exit fullscreen mode

Understanding the Problem

First, look at the output carefully.

We can notice two simple things:

  • There are 4 rows
  • Each row has 4 numbers

So the pattern is like a small square grid.

Row 1 → 1 1 1 1
Row 2 → 1 1 1 1
Row 3 → 1 1 1 1
Row 4 → 1 1 1 1
Enter fullscreen mode Exit fullscreen mode

To print something like this, we use nested loops.


What are Nested Loops?

A nested loop means a loop inside another loop.

We use two loops because:

  • One loop handles rows
  • The other loop handles columns

Simple idea:

Outer loop → rows
Inner loop → numbers inside each row
Enter fullscreen mode Exit fullscreen mode

Approach

Step 1 — Create the Row Loop

We need 4 rows, so we create a loop that runs 4 times.

for (let i = 0; i < 4; i++)
Enter fullscreen mode Exit fullscreen mode

Every time this loop runs, it means a new row will be printed.


Step 2 — Create an Empty Row

Before printing numbers, we create an empty string.

let row = "";
Enter fullscreen mode Exit fullscreen mode

This string will store the numbers for one row.


Step 3 — Create the Column Loop

Inside the first loop, we add another loop.

for (let j = 0; j < 4; j++)
Enter fullscreen mode Exit fullscreen mode

This loop runs 4 times because each row needs 4 numbers.


Step 4 — Add the Number

Inside the second loop we add "1 " to the row.

row += "1 ";
Enter fullscreen mode Exit fullscreen mode

After each loop run the row becomes:

First time  → 1
Second time → 1 1
Third time  → 1 1 1
Fourth time → 1 1 1 1
Enter fullscreen mode Exit fullscreen mode

Step 5 — Print the Row

After the inner loop finishes, we print the row.

console.log(row);
Enter fullscreen mode Exit fullscreen mode

Then the outer loop runs again to create the next row.


Final Code

for (let i = 0; i < 4; i++) {
  let row = "";

  for (let j = 0; j < 4; j++) {
    row += "1 ";
  }

  console.log(row);
}
Enter fullscreen mode Exit fullscreen mode

Final Output

1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
Enter fullscreen mode Exit fullscreen mode

Question 2

Write a program using nested loops to print the following pattern.

333
313
323
333
Enter fullscreen mode Exit fullscreen mode

Constraints:

  • Use two loops
  • The outer loop controls rows
  • The inner loop controls columns

Understanding the Pattern

The pattern has:

  • 4 rows
  • 3 columns
Row 1 → 333
Row 2 → 313
Row 3 → 323
Row 4 → 333
Enter fullscreen mode Exit fullscreen mode

We notice that:

  • The top row and bottom row are completely 3.
  • The first column and last column are also 3.
  • Only the middle values change.

Edge Cases

1️⃣ Top Row

If the outer loop is at the first row, print 3 in all columns.

Condition

i === 0
Enter fullscreen mode Exit fullscreen mode

Example

333
Enter fullscreen mode Exit fullscreen mode

2️⃣ Bottom Row

If the outer loop reaches the last row, print 3 in all columns.

Condition

i === col - 1
Enter fullscreen mode Exit fullscreen mode

Example

333
Enter fullscreen mode Exit fullscreen mode

3️⃣ First Column

For middle rows, if the inner loop index is 0, print 3.

Condition

j === 0
Enter fullscreen mode Exit fullscreen mode

Example

3 _ _
Enter fullscreen mode Exit fullscreen mode

4️⃣ Last Column

If the inner loop reaches the last column, print 3.

Condition

j === row - 1
Enter fullscreen mode Exit fullscreen mode

Example

_ _ 3
Enter fullscreen mode Exit fullscreen mode

5️⃣ Middle Position

If the position is not on the edge, print the column number.

Row 2 → 313
Row 3 → 323
Enter fullscreen mode Exit fullscreen mode

This happens using:

str += j + 1
Enter fullscreen mode Exit fullscreen mode

Final Code

let row = 3
let col = 4

for (let i = 0; i < col; i++) {
  let str = "";

  for (let j = 0; j < row; j++) {

    if (i === 0 || i === col - 1) {
      str += row;
    } 
    else {
      if (j === 0 || j === row - 1) {
        str += row;
      } 
      else {
        str += j + 1;
      }
    }

  }

  console.log(str);
}
Enter fullscreen mode Exit fullscreen mode

Output

333
313
323
333
Enter fullscreen mode Exit fullscreen mode

Question 3

Write a program using nested loops to print the following number pattern.

Expected Output

6666
555
44
3
Enter fullscreen mode Exit fullscreen mode

Explanation

  • The first row prints 4 times the number 6.
  • The second row prints 3 times the number 5.
  • The third row prints 2 times the number 4.
  • The fourth row prints 1 time the number 3.

Approach

First, look at the pattern.

6666
555
44
3
Enter fullscreen mode Exit fullscreen mode

We can see two things:

  1. The number changes in every row
  2. The number of values in each row becomes smaller

Step 1 — Outer Loop (Rows)

The outer loop runs from 0 to 3, which means it runs 4 times.

Each time the outer loop runs, one row is printed.

Example:

i = 0 → first row
i = 1 → second row
i = 2 → third row
i = 3 → fourth row
Enter fullscreen mode Exit fullscreen mode

So the outer loop decides how many rows we print.


Step 2 — Inner Loop (Columns)

At the beginning, the inner loop runs from 0 to 3.

That means the number will be printed 4 times.

Example:

6666
Enter fullscreen mode Exit fullscreen mode

Step 3 — After the First Row

When the first row is finished:

  • The number decreases
  • The number of columns also decreases

So now the inner loop runs from 0 to 2.

Output becomes:

555
Enter fullscreen mode Exit fullscreen mode

Step 4 — Continue the Same Process

Every time a row is printed:

  • The number becomes smaller
  • The inner loop runs fewer times

Example:

Row 1 → inner loop runs 4 times → 6666
Row 2 → inner loop runs 3 times → 555
Row 3 → inner loop runs 2 times → 44
Row 4 → inner loop runs 1 time  → 3
Enter fullscreen mode Exit fullscreen mode

Step 5 — How We Control This

After printing each row we update two things:

num--   → decrease the number
row--   → decrease how many times the inner loop runs
Enter fullscreen mode Exit fullscreen mode

Because of this, both the number and the column size keep decreasing.

Final Code

let col = 4
let row = col
let num = (col - 1) * 2

for (let i = 0; i < col; i++) {

  let str = "";

  for (let j = 0; j < row; j++) {
    str += `${num}`
  }

  num--
  row--

  console.log(str);
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)