JavaScript Number Triangle Pattern (Starting from 0)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

An increasing triangle from 0 uses zero-based loops — each cell is i + j, so the first row is 0 and row i starts at i.

Remember
Rule: for i = 0..maxN
        for j = 0..i
          print (i + j) + " "

0
1 2
2 3 4
3 4 5 6
4 5 6 7 8
5 6 7 8 9 10     ← maxN = 5

Twin of Program 33 (i + j - 1 from 1): same growing shape, but both loops start at 0.

How to Solve It

Walk i from 0 to maxN; on each row append i + 1 values using i + j.

MethodIdeaBest for
Nested loopsOuter i = 0..maxN; inner j = 0..i; value i + jLearning, interviews
prompt maxNSame loops; read max index at runtimeInteractive practice
Compact maxN = 2Same formula; easier to trace by handPaper walkthrough

Pseudocode

Pseudocode
for i from 0 to maxN:
    line = ""
    for j from 0 to i:
        line += (i + j) + " "
    print line (with newline)

Cheat sheet

GoalPattern
Set max indexconst maxN = 5;
Outer loopfor (let i = 0; i <= maxN; i++)
Inner loopfor (let j = 0; j <= i; j++)
Valueline += (i + j) + " ";
End rowconsole.log(line);
Row lengthi + 1 values
vs Program 33i + j - 1 with i from 1

Printing Numbers vs Starting a New Line

APIEffectUse for
line += value + " "Stays on the same rowEach number
console.log(line)Ends the lineAfter the inner loop

Build the row with +=, then break once with console.log. Putting console.log inside the inner loop prints one number per line.

Live Preview

Change the max index and the triangle from 0 updates instantly.

Whole numbers from 0 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result maxN = 5 · rows = 6 · values = 21
0
1 2
2 3 4
3 4 5 6
4 5 6 7 8
5 6 7 8 9 10

Worked Walkthrough — maxN = 2

Trace each outer i, the formula i + j, and the printed row.

iValues i+jPrinted row
00+0 = 00
11, 21 2
22, 3, 42 3 4

Each next row starts at i and adds one more consecutive value — total rows = maxN + 1.

JavaScript Programs

Three complete programs: fixed maxN = 5, prompt max index, and compact maxN = 2. Use View Output for sample results, or Try It Yourself to edit and run in the playground.

Example 1 — Fixed maxN = 5

Hard-coded max index — zero-based nested loops with formula i + j.

JavaScript
const maxN = 5;

for (let i = 0; i <= maxN; i++) {
  let line = "";
  for (let j = 0; j <= i; j++) {
    line += (i + j) + " ";
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Outer loop. i runs from 0 to 5 — six rows total.

2. Inner loop. j runs from 0 to i; append (i + j) + " ".

3. Newline. console.log(line) after the inner loop prints the row.

Example 2 — prompt Max Index

Read maxN at runtime; allow 0 as a valid minimum.

JavaScript
const maxN = parseInt(prompt("Enter max i:"), 10);

if (!Number.isFinite(maxN) || maxN < 0) {
  console.log("Please enter a non-negative integer.");
} else {
  for (let i = 0; i <= maxN; i++) {
    let line = "";
    for (let j = 0; j <= i; j++) {
      line += (i + j) + " ";
    }
    console.log(line);
  }
}
Try It Yourself

How It Works

1. Prompt and parse. Convert the answer with parseInt(..., 10).

2. Validate first. Reject NaN or negative values — 0 is allowed.

3. Same loops. Only the source of maxN changes — still print i + j on each row.

Example 3 — Compact maxN = 2

Same nested-loop formula with a smaller max — easy to trace on paper.

JavaScript
const maxN = 2;

for (let i = 0; i <= maxN; i++) {
  let line = "";
  for (let j = 0; j <= i; j++) {
    line += (i + j) + " ";
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Same structure. Outer and inner loops match Example 1.

2. Only maxN changes. Three rows; total values = 1+2+3 = 6.

3. Trace first. Walk i = 0, 1, 2 on paper before scaling to 5 or more.

Edge Cases & Pitfalls

Check these before calling the solution done.

log inside

Vertical numbers

If console.log is inside the inner loop, each value lands on its own line. Log only after the row is built.

start at 1

Missing zero row

Starting i at 1 skips the 0 row and looks like Program 33 without the - 1.

j < i

Off-by-one length

Use j <= i so row i prints i + 1 values. j < i drops the last cell.

maxN = 0

Single zero

Output is just 0 — valid and useful for testing input handling.

no space

Glued digits

Omitting + " " prints 12 instead of 1 2.

NaN input

Validate parseInt

Letters or empty prompt yield NaN — check Number.isFinite(maxN) && maxN >= 0.

Time and Space Complexity

ProgramTimeExtra space
Examples 1–3O(n²)O(n) for the current line string

For max index n, rows = n + 1 and total values = 1 + 2 + … + (n+1) = (n+1)(n+2)/2 → O(n²).

Key Takeaways

  • Rule: print i + j for j = 0..i with i from 0 to maxN.
  • Zero-based: first cell is 0; row length is i + 1.
  • Write vs log: line += … builds; console.log(line) breaks.
  • Complexity: O(n²) for max index n.

One line: start both loops at 0 and print i + j to grow a triangle that begins with zero.

Frequently Asked Questions

Because the loops start at i = 0 and j = 0, so i + j = 0.
j increases from 0 to i, so i + j increases by 1 each step — producing consecutive numbers.
Program 33 uses i + j - 1 with i starting at 1. Program 34 uses i + j with i starting at 0.
Program 34 uses the formula i + j per cell. Program 35 is a right-aligned continuous counter triangle.
line += (i + j) + " " keeps values separated on the same row. console.log(line) ends the row.
Replace 5 with maxN in the outer loop bound — see Example 2.
O(n²) for max index n because total appends are 1 + 2 + ... + (n+1) when i runs 0..n.
Use parseInt with Number.isFinite so bad input does not produce NaN. Allow 0 as a valid max.
Only one row prints — a single 0.

Did you know?

Each printed value is computed as i + j. With i = 0 and j = 0 the first row prints 0; row i = 2 prints 2, 3, 4 — a zero-based left-shifted increasing triangle.

Next: Right-Aligned Incremental Triangle

Continue with the next pattern in the JavaScript number-pattern series.

Program 35 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful