JavaScript Repeating Number Triangle Pattern (Descending)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
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.
Approach
How to Solve It
Count the digit down from n; on each row, append that digit n − i + 1 times.
Method
Idea
Best for
Nested loops
Outer i = n..1; inner appends i
Learning, interviews
prompt input
Same loops; read n at runtime
Interactive practice
String.repeat
String(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
Goal
Pattern
Set size
const n = 5;
Outer loop
for (let i = n; i >= 1; i--)
Inner loop
for (let j = n; j >= i; j--) line += i;
End row
console.log(line);
Repeat count
n - i + 1
Shortcut
console.log(String(i).repeat(n - i + 1));
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 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 resultn = 5 · digits = 15
5
44
333
2222
11111
Trace
Worked Walkthrough — n = 3
Trace each outer value of i and how many times it is printed.
i
Repeats n−i+1
Printed row
3
1× print 3
3
2
2× print 2
22
1
3× print 1
111
As i drops, the inner loop runs longer — more copies of a smaller digit.
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 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);
}
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);
}
}
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.
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: 1 + 2 + … + n = n(n+1)/2 → still O(n²).
Remember
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.