JavaScript Decreasing Number Triangle Pattern (Right-Aligned)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
A right-aligned decreasing triangle grows longer each row — each line starts at rows and counts down to i, padded so the right edge stays aligned.
Remember
Rule: for i = rows..1
indent (i-1) times with " "
print j = rows..i with padStart(2)
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1 ← rows = 5
After Program 35’s continuous counter, this one restarts from rows on every row with a separate indent loop.
Approach
How to Solve It
Count i down from rows; indent, then append rows..i with fixed-width cells.
Method
Idea
Best for
Indent + descend
Space loop, then j = rows..i with padStart(2)
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
for i from rows down to 1:
line = ""
for j from 1 to i - 1:
line += " "
for j from rows down to i:
line += pad(j, width 2)
print line
Cheat sheet
Goal
Pattern
Set rows
const rows = 5;
Outer loop
for (let i = rows; i >= 1; i--)
Indent
for (let j = 1; j < i; j++) line += " ";
Numbers
for (let j = rows; j >= i; j--)
Cell width
line += String(j).padStart(2, " ");
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(j).padStart(2, " ")
Stays on the same row
Each cell
console.log(line)
Ends the line
After both inner loops
Build the row with +=, then break once with console.log. Putting console.log inside an inner loop prints one cell per line.
Try it
Live Preview
Change the row count and the right-aligned decreasing 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
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
Trace
Worked Walkthrough — rows = 3
Trace each outer i, the indent steps, the descending values, and the printed row.
i
Indent
Numbers 3..i
Printed row
3
2 × " "
3
3
2
1 × " "
3, 2
3 2
1
none
3, 2, 1
3 2 1
Indent steps = i - 1. Every row still begins at 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 — indent loop, then descending j with padStart(2).
JavaScript
const rows = 5;
for (let i = rows; i >= 1; i--) {
let line = "";
for (let j = 1; j < i; j++) {
line += " ";
}
for (let j = rows; j >= i; j--) {
line += String(j).padStart(2, " ");
}
console.log(line);
}
1. Same structure. Outer countdown and both inner loops match Example 1.
2. Only rows changes. Total numbers = 1+2+3 = 6.
3. Trace first. Walk i = 3, 2, 1 on paper before scaling to 5 or more.
Edge Cases & Pitfalls
Check these before calling the solution done.
no indent
Snaps left
Skipping the indent loop left-aligns every row and loses the right edge.
no pad
Column drift
Plain line += j + " " misaligns when values reach two digits. Use padStart(2).
i++
Wrong outer direction
Counting i upward prints the longest row first. Keep i = rows..1.
log inside
Vertical cells
If console.log is inside either inner loop, each cell lands on its own line.
rows = 1
Single value
Output is one padded 1 with no leading spaces.
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
Indent work plus numbers per row still total O(n) cells each → O(n²). Numbers printed = n(n+1)/2.
Remember
Key Takeaways
Rule: indent i - 1 steps, then print rows..i.
padStart(2): keeps columns aligned as digits grow.
Write vs log:line += … builds; console.log(line) breaks.
Complexity:O(n²) for n rows.
One line: shrink the stop digit i while always starting from rows, and indent so the right edge stays flush.
Frequently Asked Questions
A right-aligned decreasing triangle: the first row prints 5, the next prints 5 4, then 5 4 3, and so on until 5 4 3 2 1.
An indentation loop appends two spaces while j < i before the number loop runs, pushing shorter rows to the right.
padStart(2, " ") reserves 2 columns per number (right-aligned), keeping columns stable in the console output.
The number loop runs for j from rows down to i, so every row begins at rows and counts down to the current i.
Program 35 uses a continuous counter k. Program 36 restarts from rows on each row with a separate indentation loop.
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 number with no leading spaces.
🤔
Did you know?
Each row starts from rows and counts down to i. An indentation loop appends two spaces per step (j = 1..i-1), then String(j).padStart(2, " ") keeps number columns aligned.