JavaScript Repeating Number Triangle Pattern (Descending)

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

What Is This Pattern?

A descending repeating triangle starts with one copy of the top digit and increases repeats as the digit decreases — 5, 44, 333, …

Remember
Rule: for i = n down to 1
        print i, (n − i + 1) times

5
44
333
2222
11111     ← n = 5

Twin of Program 9 (1, 22, 333): flip the outer loop so the digit shrinks while the repeat count grows.

How to Solve It

Count the digit down from n; on each row, append that digit n − i + 1 times.

MethodIdeaBest for
Nested loopsOuter i = n..1; inner appends iLearning, interviews
prompt inputSame loops; read n at runtimeInteractive practice
String.repeatString(i).repeat(n − i + 1)Shorter demos

Pseudocode

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

Cheat sheet

GoalPattern
Set sizeconst n = 5;
Outer loopfor (let i = n; i >= 1; i--)
Inner loopfor (let j = n; j >= i; j--) line += i;
End rowconsole.log(line);
Repeat countn - i + 1
Shortcutconsole.log(String(i).repeat(n - i + 1));

Printing Numbers vs Starting a New Line

APIEffectUse for
line += iStays on the same rowEach repeated 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 repeating 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
5
44
333
2222
11111

Worked Walkthrough — n = 3

Trace each outer value of i and how many times it is printed.

iRepeats n−i+1Printed row
31× print 33
22× print 222
13× print 1111

As i drops, the inner loop runs longer — more copies of a smaller digit.

JavaScript Programs

Three complete programs: fixed n = 5, prompt input, and a String.repeat shortcut. 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 picks digit; inner controls how many times to print it.

JavaScript
const n = 5;

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

How It Works

1. Outer loop. i runs from n down to 1 — the digit for that row.

2. Inner loop. j runs from n down to i, so line += i runs n − i + 1 times.

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 = n; j >= i; j--) {
      line += i;
    }
    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 — print i, n − i + 1 times.

Example 3 — String.repeat

Build each row in one call — same shape, no explicit inner loop.

JavaScript
const n = 5;

for (let i = n; i >= 1; i--) {
  console.log(String(i).repeat(n - i + 1));
}
Try It Yourself

How It Works

1. One outer loop. Still walk i from n down to 1.

2. Build the row. String(i).repeat(n − i + 1) creates the repeated digit string.

3. Print and advance. console.log prints that string and ends the 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.

print(j)

Wrong digits

Appending j instead of i prints a countdown, not a repeated digit.

Outer ascending

Program 9 shape

Counting i up from 1 with j = 1..i yields 1, 22, 333 — that is Program 9.

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
String.repeat (Example 3)O(n²)O(n) per temporary row string

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

Key Takeaways

  • Rule: outer i = n..1; print i exactly n − i + 1 times.
  • Vs Program 9: digit shrinks; repeat count grows.
  • Write vs log: line += i builds; console.log(line) breaks.
  • Complexity: O(n²) for n rows.

One line: walk the digit down from n, and print it one more time on each new row.

Frequently Asked Questions

The outer loop decreases i from n down to 1. The inner loop runs j from n down to i, so it executes once when i equals n, twice when i is n−1, and so on until n times for i = 1.
for (let j = n; j >= i; j--) runs (n − i + 1) times. That count grows as i shrinks, which is why 4 prints twice, 3 prints three times, and 1 prints n times.
line += i stays on the same row while building digits. console.log(line) prints the completed row and adds a newline.
Program 9 grows upward: row i repeats digit i exactly i times (1, 22, 333). Program 10 starts with one copy of the top digit and increases repeats as the digit decreases (5, 44, 333, 2222, 11111).
O(n²) where n is the number of rows. Total digit appends equal 1+2+…+n = n(n+1)/2.
Yes. console.log(String(i).repeat(n − i + 1)) prints a full row in one call. Nested loops are better for learning; String.repeat is a handy shortcut later.
Use parseInt(prompt(...), 10) and check Number.isFinite(n) && n >= 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 digit i repeats n − i + 1 times. The outer loop counts down from n, so the first row shows one copy of the top digit and the last row repeats 1 for n times — still O(n²) total prints.

Next: Inverted Repeating Triangle

Continue with the inverted repeating triangle in the JavaScript number-pattern series.

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