JavaScript Number Triangle Pattern (Consecutive Offset)

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

What Is This Pattern?

An increasing triangle from 1 grows one value per row — each cell is i + j - 1, so row i starts at i and counts upward.

Remember
Rule: for i = 1..rows
        for j = 1..i
          print (i + j - 1) + " "

1
2 3
3 4 5
4 5 6 7
5 6 7 8 9     ← rows = 5

Twin of Program 32 (starts at 11 with 9 + i + j): same nested shape, different formula — next is Program 34’s i + j from 0.

How to Solve It

Walk i from 1 to rows; on each row append i values using i + j - 1.

MethodIdeaBest for
Nested loopsOuter i; inner j = 1..i; value i + j - 1Learning, interviews
prompt rowsSame loops; read rows at runtimeInteractive practice
Compact rows = 3Same formula; easier to trace by handPaper walkthrough

Pseudocode

Pseudocode
for i from 1 to rows:
    line = ""
    for j from 1 to i:
        line += (i + j - 1) + " "
    print line (with newline)

Cheat sheet

GoalPattern
Set rowsconst rows = 5;
Outer loopfor (let i = 1; i <= rows; i++)
Inner loopfor (let j = 1; j <= i; j++)
Valueline += (i + j - 1) + " ";
End rowconsole.log(line);
First cell of rowi + 1 - 1 = i
vs Program 329 + i + j (starts at 11)

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 row count and the i + j - 1 triangle updates instantly.

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

Live result rows = 5 · values = 15
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9

Worked Walkthrough — rows = 3

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

iValues i+j-1Printed row
11+1-1 = 11
22, 32 3
33, 4, 53 4 5

Each next row starts at i and adds one more consecutive value.

JavaScript Programs

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

Example 1 — Fixed rows = 5

Hard-coded height — nested loops with formula i + j - 1.

JavaScript
const rows = 5;

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

How It Works

1. Outer loop. i runs from 1 to 5 — one growing row per iteration.

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

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

Example 2 — prompt Rows

Read rows at runtime with prompt and parseInt.

JavaScript
const rows = parseInt(prompt("Enter rows:"), 10);

if (!Number.isFinite(rows) || rows < 1) {
  console.log("Please enter a positive integer.");
} else {
  for (let i = 1; i <= rows; i++) {
    let line = "";
    for (let j = 1; j <= i; j++) {
      line += (i + j - 1) + " ";
    }
    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 non-positive values before looping.

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

Example 3 — Compact rows = 3

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

JavaScript
const rows = 3;

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

How It Works

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

2. Only rows changes. Total values = 1+2+3 = 6.

3. Trace first. Walk i = 1, 2, 3 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.

no space

Glued digits

Omitting + " " prints 23 instead of 2 3.

j <= rows

Wrong inner bound

Using j <= rows makes every row the same width. Keep j <= i.

rows = 1

Single value

Output is just 1.

wrong formula

Off-by-one start

Using i + j starts at 2; 9 + i + j is Program 32. Keep i + j - 1.

NaN input

Validate parseInt

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

Time and Space Complexity

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

Total values = 1 + 2 + … + n = n(n+1)/2 → O(n²).

Key Takeaways

  • Rule: print i + j - 1 for j = 1..i on each row.
  • Row start: when j = 1, the value equals i.
  • Write vs log: line += … builds; console.log(line) breaks.
  • Complexity: O(n²) for n rows.

One line: grow the row with j = 1..i and compute each cell as i + j - 1.

Frequently Asked Questions

Because when j = 1, the expression i + j - 1 becomes i. Row 4 therefore starts with 4.
j increases by 1, so i + j - 1 increases by 1 as well — producing consecutive numbers on each row.
Program 32 uses 9 + i + j (starts at 11). Program 33 uses i + j - 1 (starts at 1).
Program 33 uses i + j - 1 with i starting at 1. Program 34 uses i + j with i starting at 0.
line += (i + j - 1) + " " keeps values separated on the same row. console.log(line) ends the row.
Replace 5 with rows in the outer loop bound — see Example 2.
O(n²) for n rows because total appends are 1 + 2 + ... + n = n(n+1)/2.
Use parseInt with Number.isFinite so bad input does not produce NaN.
Only one row prints — a single 1.

Did you know?

Each printed value is computed as i + j - 1. Row i = 1 prints 1; row i = 4 prints 4, 5, 6, 7 — a left-shifted increasing triangle starting at 1.

Next: Increasing Triangle from 0

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

Program 34 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