JavaScript Incremental Number Triangle Pattern (Right-Aligned)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
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.
Approach
How to Solve It
Walk i from 1 to rows; fill a fixed-width row from the right with counter k.
Method
Idea
Best for
Counter + pad
Spaces while j > i; else padStart(3) and k++
Learning, interviews
prompt rows
Same loops; read rows at runtime
Interactive practice
Compact rows = 3
Same structure; easier to trace by hand
Paper 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
Goal
Pattern
Set rows / counter
const rows = 5; let k = 1;
Outer loop
for (let i = 1; i <= rows; i++)
Inner loop
for (let j = rows; j >= 1; j--)
Leading pad
if (j > i) line += " ";
Number cell
line += String(k).padStart(3, " "); k += 1;
End row
console.log(line);
Values printed
n(n+1)/2 for n rows
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += " " / line += String(k).padStart(3, " ")
Stays on the same row
Each cell
console.log(line)
Ends the line
After 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.
Try it
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 resultrows = 5 · values = 15
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Trace
Worked Walkthrough — rows = 3
Trace each outer i, how many space groups, the values of k, and the printed row.
i
Space groups
Numbers (k)
Printed row
1
2 × " "
1
1
2
1 × " "
2, 3
2 3
3
none
4, 5, 6
4 5 6
Space groups = rows - i. Counter k never resets between rows.
Code
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);
}
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);
}
}
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);
}
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.
Analysis
Time and Space Complexity
Program
Time
Extra space
Examples 1–3
O(n²)
O(n) for the current line string
n rows of width n (spaces or numbers) → O(n²). Numbers printed = n(n+1)/2.
Remember
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.