JavaScript Repeating Number Triangle Pattern (Inverted)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
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.
Approach
How to Solve It
Count the digit down from n; on each row, append that digit exactly i times.
Method
Idea
Best for
Nested loops
Outer i = n..1; inner appends i times
Learning, interviews
prompt input
Same loops; read n at runtime
Interactive practice
String.repeat
String(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
Goal
Pattern
Set size
const n = 5;
Outer loop
for (let i = n; i >= 1; i--)
Inner loop
for (let j = 1; j <= i; j++) line += i;
End row
console.log(line);
Repeat count
i (same as the digit)
Shortcut
console.log(String(i).repeat(i));
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += i
Stays on the same row
Each repeated digit
console.log(line)
Ends the line
After 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.
Try it
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 resultn = 5 · digits = 15
55555
4444
333
22
1
Trace
Worked Walkthrough — n = 3
Trace each outer value of i and how many times it is printed.
i
Repeats i
Printed row
3
3× print 3
333
2
2× print 2
22
1
1× print 1
1
As i drops, both the digit and the repeat count shrink together.
Code
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);
}
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);
}
}
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.
Analysis
Time and Space Complexity
Program
Time
Extra space
Examples 1–2
O(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²).
Remember
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.