JavaScript Incremental Number Triangle Pattern (Right-Aligned)

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

What Is This Pattern?

A right-aligned incremental triangle prints a continuous sequence — 1, then 2 3, then 4 5 6 — indented so each row lines up on the right.

Remember
Rule: k = 1; for i = 1..rows
      for j = rows..1:
        if j > i → "   " else padStart(k) and k++

              1
           2  3
        4  5  6
     7  8  9 10
 11 12 13 14 15     ← rows = 5

After Program 34’s i + j triangle, this one adds a continuous counter and fixed-width right alignment.

How to Solve It

Walk i from 1 to rows; fill a fixed-width row from the right with counter k.

MethodIdeaBest for
Counter + padSpaces while j > i; else padStart(3) and k++Learning, interviews
prompt rowsSame loops; read rows at runtimeInteractive practice
Compact rows = 3Same structure; easier to trace by handPaper walkthrough

Pseudocode

Pseudocode
k = 1
for i from 1 to rows:
    line = ""
    for j from rows down to 1:
        if j > i:
            line += "   "
        else:
            line += pad(k, width 3); k += 1
    print line

Cheat sheet

GoalPattern
Set rows / counterconst rows = 5; let k = 1;
Outer loopfor (let i = 1; i <= rows; i++)
Inner loopfor (let j = rows; j >= 1; j--)
Leading padif (j > i) line += " ";
Number cellline += String(k).padStart(3, " "); k += 1;
End rowconsole.log(line);
Values printedn(n+1)/2 for n rows

Printing Numbers vs Starting a New Line

APIEffectUse for
line += " " / line += String(k).padStart(3, " ")Stays on the same rowEach cell
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 cell per line.

Live Preview

Change the row count and the right-aligned incremental 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
        4  5  6
     7  8  9 10
 11 12 13 14 15

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 — fixed-width inner loop, continuous k, and padStart(3).

JavaScript
let k = 1;
const rows = 5;

for (let i = 1; i <= rows; i++) {
  let line = "";
  for (let j = rows; j >= 1; j--) {
    if (j > i) {
      line += "   ";
    } else {
      line += String(k).padStart(3, " ");
      k += 1;
    }
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Outer loop. i runs from 1 to 5 — how many number cells appear on the row.

2. Fixed-width inner. j runs from 5 down to 1; while j > i append three spaces.

3. Continuous k. Otherwise append String(k).padStart(3, " ") and bump k; then console.log(line).

Example 2 — prompt Rows

Read rows at runtime; the inner loop uses that value as the fixed width.

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

if (!Number.isFinite(rows) || rows < 1) {
  console.log("Please enter a positive integer.");
} else {
  let k = 1;
  for (let i = 1; i <= rows; i++) {
    let line = "";
    for (let j = rows; j >= 1; j--) {
      if (j > i) {
        line += "   ";
      } else {
        line += String(k).padStart(3, " ");
        k += 1;
      }
    }
    console.log(line);
  }
}
Try It Yourself

How It Works

1. Prompt and validate. Parse the answer; reject non-positive or non-numeric input.

2. Same shape. Inner loop still runs j = rows..1 — only the bound changes.

3. Counter still global. k starts at 1 once and continues across all rows.

Example 3 — Compact rows = 3

Same counter and spacing with a smaller row count — easy to trace on paper.

JavaScript
const rows = 3;
let k = 1;

for (let i = 1; i <= rows; i++) {
  let line = "";
  for (let j = rows; j >= 1; j--) {
    if (j > i) {
      line += "   ";
    } else {
      line += String(k).padStart(3, " ");
      k += 1;
    }
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Same structure. Outer, inner, and padStart match Example 1.

2. Only rows changes. Total numbers = 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.

reset k

Counter restarts

Setting k = 1 inside the outer loop restarts every row at 1. Keep k outside both loops.

no pad

Column drift

Plain line += k + " " misaligns once values reach two digits. Use padStart(3).

flip test

Wrong space condition

Using j <= i for spaces (or the opposite) breaks right alignment.

log inside

Vertical cells

If console.log is inside the inner loop, each cell lands on its own line.

rows = 1

Single value

Output is one padded 1 with no leading space groups.

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

n rows of width n (spaces or numbers) → O(n²). Numbers printed = n(n+1)/2.

Key Takeaways

  • Rule: pad left while j > i; print continuous k with fixed width.
  • padStart(3): keeps columns aligned when k becomes two digits.
  • Write vs log: line += … builds; console.log(line) breaks.
  • Complexity: O(n²) for n rows.

One line: keep a running counter and pad empty columns so the continuous sequence sits flush on the right.

Frequently Asked Questions

Numbers keep increasing across rows without resetting — row 1 prints 1, row 2 prints 2 3, row 3 prints 4 5 6, and so on.
Before printing numbers on each row, the program appends three spaces while j > i. This indents the left side so numbers shift right.
padStart(3, " ") reserves 3 columns per number (right-aligned), keeping columns aligned when values become two digits.
k is set to 1 before the loops and increases with k += 1 each time a number is appended, so the sequence continues across rows.
Program 34 uses the formula i + j per cell. Program 35 uses a continuous counter k with fixed-width right alignment.
Replace 5 with rows in the outer loop bound — see Example 2.
O(n²) for n rows because total numbers printed 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 with no leading space groups.

Did you know?

A counter k starts at 1 and increments every time a number is printed. Leading spaces appear while j > i, and String(k).padStart(3, " ") keeps columns aligned as values grow past single digits.

Next: Right-Aligned Decreasing Triangle

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

Program 36 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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