0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Print Number Pattern

Use nested for loops in JavaScript to print a number pattern, for example: for(let i=1; i<=5; i++){ let line=''; for(let j=1; j<=i; j++){ line += j + ' '; } console.log(line); } prints numbers in increasing order per line.
📋

Examples

Input5
Output1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Input3
Output1 1 2 1 2 3
Input1
Output1
🧠

How to Think About It

To print a number pattern, think of rows and columns. Each row prints numbers starting from 1 up to the row number. Use one loop to go through each row and a nested loop inside it to print numbers from 1 to the current row number.
📐

Algorithm

1
Get the number of rows as input.
2
Start a loop from 1 to the number of rows (each iteration is a row).
3
Inside this loop, start another loop from 1 to the current row number.
4
In the inner loop, print the current number followed by a space.
5
After the inner loop ends, move to the next line.
6
Repeat until all rows are printed.
💻

Code

javascript
const n = 5;
for (let i = 1; i <= n; i++) {
  let line = '';
  for (let j = 1; j <= i; j++) {
    line += j + ' ';
  }
  console.log(line);
}
Output
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
🔍

Dry Run

Let's trace the example where n=3 through the code.

1

Start outer loop with i=1

Inner loop runs j=1 to 1, line becomes '1 '

2

Print line for i=1

Output: '1 '

3

Outer loop i=2

Inner loop j=1 to 2, line becomes '1 2 '

4

Print line for i=2

Output: '1 2 '

5

Outer loop i=3

Inner loop j=1 to 3, line becomes '1 2 3 '

6

Print line for i=3

Output: '1 2 3 '

i (row)j (column)line content
111
211
221 2
311
321 2
331 2 3
💡

Why This Works

Step 1: Outer loop controls rows

The outer for loop runs from 1 to n, each iteration represents a new row.

Step 2: Inner loop prints numbers

The inner for loop runs from 1 to the current row number, printing numbers in sequence.

Step 3: Building the line string

Numbers are added to a string with spaces, then printed after the inner loop ends to form the pattern line.

🔄

Alternative Approaches

Using while loops
javascript
const n = 5;
let i = 1;
while (i <= n) {
  let j = 1;
  let line = '';
  while (j <= i) {
    line += j + ' ';
    j++;
  }
  console.log(line);
  i++;
}
Uses while loops instead of for loops; slightly longer but same logic.
Printing pattern with decreasing numbers
javascript
const n = 5;
for (let i = 1; i <= n; i++) {
  let line = '';
  for (let j = i; j >= 1; j--) {
    line += j + ' ';
  }
  console.log(line);
}
Prints numbers in each row in decreasing order from the row number down to 1.

Complexity: O(n^2) time, O(n) space

Time Complexity

The nested loops run roughly n*(n+1)/2 times, which is O(n^2), because for each row i, the inner loop runs i times.

Space Complexity

We use a string to build each line, which takes O(n) space for the longest line; no extra large data structures are used.

Which Approach is Fastest?

All approaches have similar O(n^2) time; using for loops is more concise and readable than while loops.

ApproachTimeSpaceBest For
Nested for loopsO(n^2)O(n)Simple and readable pattern printing
Nested while loopsO(n^2)O(n)When you prefer while loops or need more control
Decreasing number patternO(n^2)O(n)Different pattern style, same complexity
💡
Use nested loops where the outer loop controls rows and the inner loop controls columns for patterns.
⚠️
Beginners often forget to reset the line string inside the outer loop, causing all lines to concatenate.