JavaScript Number Triangle Pattern (Starting from 11)

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

What Is This Pattern?

An increasing triangle from 11 grows one value per row — each cell is base + i + j (with base 9), so the first row is 11.

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

11
12 13
13 14 15
14 15 16 17
15 16 17 18 19     ← rows = 5

A natural step after Program 31’s number-star diamond — here you practice a formula-driven triangle (compare Program 33’s i + j - 1).

How to Solve It

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

MethodIdeaBest for
Nested loopsOuter i; inner j = 1..i; value 9 + i + jLearning, interviews
prompt base + rowsSame loops; replace 9 with baseValInteractive 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 += (9 + i + j) + " "
    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 += (9 + i + j) + " ";
End rowconsole.log(line);
Custom baseline += (baseVal + i + j) + " ";
First cell9 + 1 + 1 = 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 triangle from 11 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
11
12 13
13 14 15
14 15 16 17
15 16 17 18 19

Worked Walkthrough — rows = 3

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

iValues 9+i+jPrinted row
19+1+1 = 1111
212, 1312 13
313, 14, 1513 14 15

Each next row starts one higher and adds one more value.

JavaScript Programs

Three complete programs: fixed rows = 5, custom base + rows via prompt, 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 9 + i + j.

JavaScript
const rows = 5;

for (let i = 1; i <= rows; i++) {
  let line = "";
  for (let j = 1; j <= i; j++) {
    line += (9 + i + j) + " ";
  }
  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 (9 + i + j) + " ".

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

Example 2 — Custom Base and Rows

Read rows and baseVal at runtime; use baseVal + i + j instead of hard-coded 9.

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

if (!Number.isFinite(rows) || rows < 1 || !Number.isFinite(baseVal)) {
  console.log("Please enter valid integers.");
} else {
  for (let i = 1; i <= rows; i++) {
    let line = "";
    for (let j = 1; j <= i; j++) {
      line += (baseVal + i + j) + " ";
    }
    console.log(line);
  }
}
Try It Yourself

How It Works

1. Prompt twice. Parse rows and base; reject invalid values before looping.

2. Same shape. Inner loop still runs j = 1..i — only the formula offset changes.

3. Shift freely. Base 0 starts at 2; base 9 starts at 11.

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 += (9 + i + j) + " ";
  }
  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 1213 instead of 12 13.

j <= rows

Wrong inner bound

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

rows = 1

Single value

Output is just 11.

wrong offset

Off-by-one start

Using 10 + i + j starts at 12; i + j - 1 is Program 33’s formula.

NaN input

Validate parseInt

Letters or empty prompt yield NaN — check Number.isFinite for both rows and base.

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 9 + i + j for j = 1..i on each row.
  • Base 9: first cell is 11; change the base to shift the triangle.
  • 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 base + i + j.

Frequently Asked Questions

Because the printed value is 9 + i + j. On the first row i = 1 and j = 1, so 9 + 1 + 1 = 11.
It is a base offset. Change 9 to any base value to shift the entire triangle — see Example 2.
Program 32 uses 9 + i + j (starts at 11). Program 33 uses i + j - 1 (starts at 1).
line += (9 + i + j) + " " 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 11.
Yes — line += (baseVal + i + j) + " " lets the user pick any starting offset.

Did you know?

Each printed value is computed as 9 + i + j. Row i = 1 appends 11; row i = 2 appends 12 and 13 — a left-shifted increasing triangle.

Next: Increasing Triangle from 1

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

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