JavaScript Decreasing Number Triangle Pattern (Right-Aligned)

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

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.

How to Solve It

Count i down from rows; indent, then append rows..i with fixed-width cells.

MethodIdeaBest for
Indent + descendSpace loop, then j = rows..i with padStart(2)Learning, interviews
prompt rowsSame loops; read rows at runtimeInteractive practice
Compact rows = 3Same structure; easier to trace by handPaper 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

GoalPattern
Set rowsconst rows = 5;
Outer loopfor (let i = rows; i >= 1; i--)
Indentfor (let j = 1; j < i; j++) line += " ";
Numbersfor (let j = rows; j >= i; j--)
Cell widthline += String(j).padStart(2, " ");
End rowconsole.log(line);
Values printedn(n+1)/2 for n rows

Printing Numbers vs Starting a New Line

APIEffectUse for
line += " " / line += String(j).padStart(2, " ")Stays on the same rowEach cell
console.log(line)Ends the lineAfter 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.

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 result rows = 5 · values = 15
         5
       5 4
     5 4 3
   5 4 3 2
 5 4 3 2 1

Worked Walkthrough — rows = 3

Trace each outer i, the indent steps, the descending values, and the printed row.

iIndentNumbers 3..iPrinted row
32 × " "33
21 × " "3, 23 2
1none3, 2, 13 2 1

Indent steps = i - 1. Every row still begins at rows.

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);
}
Try It Yourself

How It Works

1. Outer countdown. i runs from 5 down to 1 — the last digit of that row.

2. Indent. Append " " while j < i so shorter rows shift right.

3. Descend. Append String(j).padStart(2, " ") for j = rows..i; then console.log(line).

Example 2 — prompt Rows

Read rows at runtime; both inner loops use that value as the start digit and width.

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 = 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);
  }
}
Try It Yourself

How It Works

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

2. Same shape. Indent and descend still use rows — only the bound changes.

3. Restart each row. Unlike Program 35, every row begins again at rows.

Example 3 — Compact rows = 3

Same indent + descend structure with a smaller row count — easy to trace on paper.

JavaScript
const rows = 3;

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);
}
Try It Yourself

How It Works

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.

Time and Space Complexity

ProgramTimeExtra space
Examples 1–3O(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.

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.

Next: Palindrome Number Triangle

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

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