JavaScript Repeating Number Triangle Pattern (Inverted)

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

What Is This Pattern?

An inverted repeating triangle starts wide and shrinks: digit i repeats exactly i times — 55555, 4444, 333, …

Remember
Rule: for i = n down to 1
        print i, exactly i times

55555
4444
333
22
1         ← n = 5

Twin of Program 9 (1, 22, 333): same repeat rule, but the outer loop runs downward so the widest row comes first.

How to Solve It

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

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

Pseudocode

Pseudocode
for i from n down to 1:
    line = ""
    for j from 1 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 = 1; j <= i; j++) line += i;
End rowconsole.log(line);
Repeat counti (same as the digit)
Shortcutconsole.log(String(i).repeat(i));

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 inverted 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
55555
4444
333
22
1

Worked Walkthrough — n = 3

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

iRepeats iPrinted row
33× print 3333
22× print 222
11× print 11

As i drops, both the digit and the repeat count shrink together.

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 prints it i times.

JavaScript
const n = 5;

for (let i = n; i >= 1; i--) {
  let line = "";
  for (let j = 1; 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 1 to i, so line += i runs exactly i 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 = 1; 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 exactly i 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(i));
}
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(i) 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.

Wrong bounds

Program 10 shape

Using j = n..i instead of j = 1..i yields 5, 44, 333 — that is Program 10.

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: n + (n−1) + … + 1 = n(n+1)/2 → still O(n²).

Key Takeaways

  • Rule: outer i = n..1; print i exactly i times.
  • Vs Program 9: same repeat rule, but widest row first.
  • 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 as many times as its own value.

Frequently Asked Questions

The outer loop decreases i from n down to 1, and the inner loop runs j from 1 to i. Row i repeats digit i exactly i times, so the widest row is first and each next row is shorter.
for (let j = 1; j <= i; j++) line += i runs the inner body i times. When i is 5 you get 55555; when i is 1 you get a single 1.
line += i stays on the same row while building digits. console.log(line) prints the completed row and adds a newline.
Program 10 grows repeat count as the digit shrinks (5, 44, 333) using j from n down to i. Program 11 repeats digit i exactly i times (55555, 4444, 333, 22, 1) using j from 1 to i.
O(n²) where n is the number of rows. Total digit appends equal n+(n−1)+…+1 = n(n+1)/2.
Yes. console.log(String(i).repeat(i)) 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 exactly i times. The outer loop counts down from n, so the first row is widest (n copies of n) and each line shortens — still O(n²) total prints.

Next: Ascending Repeating Shrinking

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

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