JavaScript Descending Number Triangle Pattern

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

What Is This Pattern?

A descending number triangle prints digits 1..i on each row, with the outer loop counting down so the first row is longest and each next row shortens by one digit.

Remember
Rule: for i = n down to 1
        print j from 1 to i

12345
1234
123
12
1         ← n = 5

Classic first console pattern in JavaScript labs — once nested loops and line += j / console.log(line) click, other number triangles get easier.

How to Solve It

Count the row width down from n; on each row, append digits 1..i, then log the line.

MethodIdeaBest for
Nested loopsOuter i = n..1; build line with += jLearning, interviews
prompt inputSame loops; read n at runtimeInteractive practice
document.writePrint digits + <br> in the browserQuick HTML demos

Pseudocode

Pseudocode
for i from n down to 1:
    line = ""
    for j from 1 to i:
        line += j
    print line (with newline)

Cheat sheet

GoalPattern
Set sizeconst n = 5;
Outer loopfor (let i = n; i >= 1; i--)
Inner loopfor (let j = 1; j <= i; j++) line += j;
End rowconsole.log(line);
Digits on row iExactly 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 n and the descending number triangle updates instantly.

Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result n = 5 · digits = 15
12345
1234
123
12
1

Worked Walkthrough — n = 3

Trace each outer value of i and the digits printed on that row.

iInner jPrinted row
31 2 3123
21 212
111

Each step lowers i by 1, so the row loses its last digit.

JavaScript Programs

Three complete programs: fixed n = 5, prompt input, and a browser document.write version. Use View Output for sample results, or Try It Yourself to edit and run in the playground.

Example 1 — Fixed n = 5

Hard-coded height — outer loop counts down; each row builds digits 1..i.

JavaScript
const n = 5;

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

How It Works

1. Outer loop. i starts at n and counts down to 1 — row width shrinks each time.

2. Inner loop. Append j from 1 to i onto line — first row is 12345, then 1234, and so on.

3. Newline. console.log(line) after the inner loop prints the row and starts the next one.

Example 2 — prompt Input

Read n at runtime with prompt and parseInt.

JavaScript
const n = parseInt(prompt("Enter the number of rows:"), 10);

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

How It Works

1. Prompt and parse. Convert the answer with parseInt(..., 10).

2. Validate first. Reject NaN or non-positive values before looping.

3. Same loops. Only the source of n changes — outer down, inner 1..i.

Example 3 — document.write

Same shape in the browser — print each digit, then a line break.

JavaScript
<!DOCTYPE html>
<html>
<body>
<script>
const n = 5;

for (let i = n; i >= 1; i--) {
  for (let j = 1; j <= i; j++) {
    document.write(j);
  }
  document.write("<br>");
}
</script>
</body>
</html>
Try It Yourself

How It Works

1. Same bounds. Outer n..1, inner 1..i — identical to Example 1.

2. Write digits. document.write(j) prints each digit into the page.

3. Break the row. document.write("<br>") after the inner loop starts a new line.

Edge Cases & Pitfalls

Check these before calling the solution done.

log inside

Vertical digits

If console.log is inside the inner loop, each digit lands on its own line. Append with +=; log only after the inner loop.

Outer ascending

Wrong order

Counting i up from 1 prints the shortest row first — an ascending triangle, not this pattern.

Reuse line

Growing leftovers

Reset line = "" at the start of each outer iteration, or digits from previous rows stick around.

n = 1

Single digit

Output is just 1 on one line.

n ≤ 0

Empty output

The outer loop never runs. Guard prompt input with n >= 1.

NaN input

Validate parseInt

Letters or empty prompt yield NaN — check Number.isFinite(n) && n >= 1.

Time and Space Complexity

ProgramTimeExtra space
Examples 1–2O(n²)O(n) for the current line string
document.write (Example 3)O(n²)O(1) beyond the page itself

Total digits printed: 1 + 2 + … + n = n(n+1)/2 → still O(n²).

Key Takeaways

  • Rule: outer i = n..1; append j = 1..i on each row.
  • Shrink: first row is longest; each next row drops one digit.
  • Write vs log: line += j builds; console.log(line) breaks.
  • Complexity: O(n²) for n rows.

One line: start at width n and walk down — each row prints 1 through the current width.

Frequently Asked Questions

The outer loop runs i from n down to 1. For each row i, the inner loop runs j from 1 to i and appends j to a line string. The first row prints 1..n; each next row prints one fewer digit.
Counting down makes the first row the longest. for (let i = n; i >= 1; i--) sets i to the full width first, then shrinks by one each line.
line += j stays on the same row while building digits. console.log(line) prints the completed row and adds a newline.
Change the outer loop to for (let i = 1; i <= n; i++). Keep the inner loop as j = 1..i. The first row then has one digit and the last row has n digits.
O(n²) where n is the number of rows. Total digit appends equal 1+2+…+n = n(n+1)/2.
Yes in browser demos. document.write(j) prints each digit and document.write("<br>") ends the row. Prefer console.log or DOM updates in production code.
Use parseInt(prompt(...), 10) and check Number.isFinite(rows) && rows >= 1 so bad input does not produce NaN.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.

Did you know?

Row i prints digits 1 through i. The outer loop counts down from n, so the first line is longest and each row shortens by one digit — still O(n²) total prints.

Next: Shifted Number Triangle

Continue with the shifted number triangle in the JavaScript number-pattern series.

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