JavaScript Descending Number Triangle Pattern (Left-Aligned)

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

What Is This Pattern?

A left-aligned descending triangle prints rows that always start at the maximum digit and count down — but each row stops one digit sooner, so the triangle shrinks from the right.

Remember
Rule: for i = 0..rows-1
      print rows down to i+1

54321
5432
543
54
5           ← rows = 5

Unlike Program 3’s i..1 rows (starting digit shrinks), every row here begins at rows.

How to Solve It

Walk i from 0 to rows - 1; always start the inner loop at rows and stop after i.

MethodIdeaBest for
Fixed startInner j = rows..i+1 via j > iLearning, interviews
prompt rowsSame loops; read rows at runtimeInteractive practice
Spaced digitsSame bounds; line += j + " "Readable console output

Pseudocode

Pseudocode
for i from 0 to rows - 1:
    line = ""
    for j from rows down while j > i:
        line += j
    print line

Cheat sheet

GoalPattern
Set rowsconst rows = 5;
Outer loopfor (let i = 0; i < rows; i++)
Inner loopfor (let j = rows; j > i; j--)
Append digitline += j;
End rowconsole.log(line);
Digits per rowrows - i
vs Program 3Always start at rows vs start at i

Printing Numbers vs Starting a New Line

APIEffectUse for
line += jStays on the same rowEach digit
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 digit per line.

Live Preview

Change the row count and the descending 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 · digits = 15
54321
5432
543
54
5

Worked Walkthrough — rows = 3

Trace each outer i, the inner j values, and the printed row.

iInner j (j > i)Printed row
03 2 1321
13 232
233

Every row starts at 3. Digits per row = rows - i. Total = 6 = 3×4/2.

JavaScript Programs

Three complete programs: fixed rows = 5, prompt rows, and spaced digits. 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 — inner loop always starts at rows and shrinks the stop.

JavaScript
const rows = 5;

for (let i = 0; i < rows; i++) {
  let line = "";
  for (let j = rows; j > i; j--) {
    line += j;
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Outer loop. i runs from 0 to 4 — it only controls where the inner loop stops.

2. Inner loop. j always starts at 5 and continues while j > i, so row 0 prints five digits and the last row prints one.

3. End row. console.log(line) after the inner loop starts the next line.

Example 2 — prompt Rows

Read rows at runtime with prompt and parseInt.

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 = 0; i < rows; i++) {
    let line = "";
    for (let j = rows; j > i; j--) {
      line += j;
    }
    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. The inner bound still uses rows and i — only the height changes.

3. Start stays max. Every row still begins at the chosen rows value.

Example 3 — Spaced Digits

Same loop bounds; append a space after each digit for easier reading.

JavaScript
const rows = 5;

for (let i = 0; i < rows; i++) {
  let line = "";
  for (let j = rows; j > i; j--) {
    line += j + " ";
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Same structure. Outer and inner bounds match Example 1.

2. Only the append changes. line += j + " " adds a trailing space after each digit.

3. Shape unchanged. Still left-aligned and shrinking from the right.

Edge Cases & Pitfalls

Check these before calling the solution done.

fixed stop

Full rectangle

If the inner loop always runs to 1, every row prints the same length. The stop must depend on i.

wrong outer

Program 3 shape

A descending outer loop with j = i..1 yields Program 3, not this fixed-start triangle.

log inside

Vertical digits

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

rows = 1

Single digit

Output is just 1 — one outer iteration, one inner append.

multi-digit

Values above 9

When rows > 9, cells become two characters. Keep demos at rows ≤ 9 or use spacing.

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

Digits per pattern = n + (n-1) + … + 1 = n(n+1)/2 → O(n²).

Key Takeaways

  • Rule: always start at rows; stop sooner each row with j > i.
  • Left-aligned: no leading spaces — the triangle shrinks from the right.
  • Write vs log: line += j builds; console.log(line) breaks.
  • Complexity: total digits = n(n+1)/2 → O(n²).

One line: start every row at the max digit and drop one from the end each time.

Frequently Asked Questions

Because the inner loop always begins at rows (the maximum digit) and counts down. Only the stopping point changes per row.
Row i (0-based) appends rows - i digits — the inner loop runs for (let j = rows; j > i; j--).
Program 3 appends i..1 with a descending outer loop (4321, 321, …). Program 4 appends rows..i+1 with an ascending outer loop — every row starts at rows.
No — digits are left-aligned with no leading spaces. Each row begins flush left at the maximum digit.
Replace 5 with rows in the outer loop bound — see Example 2.
Use line += j + " " instead of line += j — see Example 3.
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 digit matching rows.

Did you know?

Each row starts at rows and counts down to a shrinking limit. Row i (0-based) appends rows - i digits — total appends = n(n+1)/2; output is left-aligned with no leading spaces.

Next: Ascending Number Triangle

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

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