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
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
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
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++)
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 = "";
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++)
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 ";
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
Step 5 — Print the Row
After the inner loop finishes, we print the row.
console.log(row);
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);
}
Final Output
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
Question 2
Write a program using nested loops to print the following pattern.
333
313
323
333
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
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
Example
333
2️⃣ Bottom Row
If the outer loop reaches the last row, print 3 in all columns.
Condition
i === col - 1
Example
333
3️⃣ First Column
For middle rows, if the inner loop index is 0, print 3.
Condition
j === 0
Example
3 _ _
4️⃣ Last Column
If the inner loop reaches the last column, print 3.
Condition
j === row - 1
Example
_ _ 3
5️⃣ Middle Position
If the position is not on the edge, print the column number.
Row 2 → 313
Row 3 → 323
This happens using:
str += j + 1
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);
}
Output
333
313
323
333
Question 3
Write a program using nested loops to print the following number pattern.
Expected Output
6666
555
44
3
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
We can see two things:
- The number changes in every row
- 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
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
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
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
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
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);
}







Top comments (0)