A reverse left-growing number triangle always starts each row at the peak digit rows, then counts down farther each time — 5, 54, 543, and so on.
Remember
Rule: for i from rows down to 1
for j from rows down to i: append j (no space)
5
54
543
5432
54321 ← rows = 5
Follows the reverse-row triangle in Program 7; next is the repeating number triangle in Program 9.
Approach
How to Solve It
Outer loop counts down from rows to 1. Inner loop appends digits rows..i. End each row with console.log(line).
Method
Idea
Best for
Descending outer + descending inner
Outer i = rows..1; inner j = rows..i; append j
Learning, interviews, exams
Spaced digits
line += j + " " instead of line += j
Easier reading on each row
Pseudocode
Pseudocode
for i from rows down to 1:
line = ""
for j from rows down to i:
line += j
print line
Cheat sheet
Goal
Pattern
Pick each row
for (let i = rows; i >= 1; i--)
Append digits
for (let j = rows; j >= i; j--) line += j;
End of row
console.log(line);
Spaced form
line += j + " ";
vs Program 6 / 7
6: i..rows up; 7: i..1; 8: rows..i down
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += j
Stays on the same row
Each digit on the row
console.log(line)
Ends the current row
After the inner loop finishes
Append digits without a newline, then end the row once.
Try it
Live Preview
Change the row count and the reverse left-growing triangle updates instantly — capped at 9 for readable single-digit demos.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 15 digits
5
54
543
5432
54321
Trace
Worked Walkthrough
Trace rows = 5 — every row starts at 5 while the stopping digit i moves leftward.
i
Inner j
Prints
5
5..5
5
4
5..4
54
3
5..3
543
1
5..1
54321
Total digits = n(n+1)/2 → O(n²). Same outer loop as Program 6; opposite inner direction.
Code
JavaScript Programs
Three complete programs: fixed rows = 5, prompt input, and a compact rows = 3 demo. Use View Output for samples, or Try It Yourself to edit and run live.
Example 1 — Fixed rows = 5
Hard-coded height — outer counts down, inner appends rows..i with no spaces.
JavaScript
const rows = 5;
for (let i = rows; i >= 1; i--) {
let line = "";
for (let j = rows; j >= i; j--) {
line += j;
}
console.log(line);
}
1. Outer down.i starts at 5 and moves toward 1 — that extends how far the countdown goes.
2. Inner from peak.j always starts at rows, so every row begins with the same digit.
3. Newline. Call console.log(line) only after the inner loop finishes the row.
Example 2 — User Input Rows
Read rows with prompt, validate, then run the same nested loops.
JavaScript
const rows = parseInt(prompt("Enter the number of rows:"), 10);
if (!Number.isFinite(rows) || rows <= 0) {
console.log("Please enter a positive integer.");
} else {
for (let i = rows; i >= 1; i--) {
let line = "";
for (let j = rows; j >= i; j--) {
line += j;
}
console.log(line);
}
}
2. Trace on paper. Confirm every row starts with 3 — only the end digit changes.
3. Scale up next. Once the small demo is clear, use Examples 1–2 for five rows or user input.
Edge Cases & Pitfalls
Check these before calling the solution done.
j = i..rows
Got Program 6 instead
If the inner loop counts up from i to rows, you get 5, 45, 345… Keep j = rows; j >= i; j--.
i = 1..rows
Wrong outer direction
An ascending outer with inner i..1 is Program 7. Program 8 needs outer rows..1 and inner rows..i.
log inside
Broken rows
If console.log sits inside the inner loop, each digit lands on its own line. Call it only after the row finishes.
rows = 1
Single digit
Output is just 1 — a good sanity check for input validation.
rows > 9
Multi-digit width
Digits 10+ make tight rows hard to read. Cap demos at 9 or switch to spaced j + " ".
Bad input
Validate parseInt
Check Number.isFinite(rows) && rows > 0 before the loops.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / compact (Examples 1, 3)
O(n²)
O(1)
User input (Example 2)
O(n²)
O(1)
Total digits appended = 1 + 2 + … + n = n(n+1)/2 → O(n²) time. Only a few loop variables are needed.
Remember
Key Takeaways
Always start at peak: outer rows..1, inner rows..i.
Same first digit: every row begins with rows; only the end digit moves left.
Break the row:line += j in the inner loop; console.log(line) after it.
Complexity:O(n²) time from n(n+1)/2 digits; O(1) extra space.
One line: count i down from rows, append j from rows down to i, then log the line.
Frequently Asked Questions
A reverse left-growing triangle: row 1 shows 5, row 2 shows 54, row 3 shows 543, and so on until the last row shows digits from rows down to 1.
The inner loop always starts at j = rows. Only the stopping value i changes, so each row begins with the peak digit.
When i = 1, the inner loop appends j from rows down to 1.
Program 7 outer counts up and logs i..1 (1, 21, 321). Program 8 outer counts down and inner always starts at rows (5, 54, 543).
Program 6 inner logs i..rows ascending (5, 45, 345). Program 8 inner logs rows..i descending (5, 54, 543).
line += appends digits on the same row. console.log(line) ends the row after the inner loop finishes.
Change rows or read it from user input with prompt() and parseInt — see Example 2.
Yes — use for (let j = i; j <= rows; j++) in the inner loop like Program 6.
O(n²) for n rows because total appends are 1 + 2 + ... + n = n(n+1)/2.
Use parseInt with Number.isFinite after prompt(). Bare parseInt(prompt()) returns NaN on bad input.
🤔
Did you know?
Each row starts at rows and counts down to i — outer i runs rows..1, inner j appends rows..i — producing 5, 54, 543, and so on. Total appends grow as O(n²).