Unlike Program 5 (append 1..i while counting up), this counts the outer loop down and appends i..rows — total digits = n(n+1)/2.
Approach
How to Solve It
Outer loop counts down from rows; inner loop appends from the current start digit up to rows.
Method
Idea
Best for
Descending outer
i = rows..1, then append i..rows
Classic reverse-ascending demos
Compact
Same logic with fewer rows (e.g. 3)
Quick dry-runs
Pseudocode
Pseudocode
for i from rows down to 1:
line = ""
for j from i to rows:
append j
print line
Cheat sheet
Goal
Pattern
Outer (rows)
for (let i = rows; i >= 1; i--)
Inner (digits)
for (let j = i; j <= rows; j++) line += j;
End the row
console.log(line);
Digits on first row
1 (just rows)
Digits on last row
rows (1..rows)
Total digits
rows * (rows + 1) / 2
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += …
Stays on the same row
Each digit
console.log(line)
Ends the current row
After the inner loop finishes
Append without a newline, then end the row once.
Try it
Live Preview
Change the row count and the reverse ascending triangle updates instantly.
Whole numbers from 1 to 9 (single digits stay neat). Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 15 digits
5
45
345
2345
12345
Trace
Worked Walkthrough
Trace three rows when rows = 5 — watch the start digit fall while the end stays fixed.
Row start i
Inner j
Printed row
5
5..5
5
3
3..5
345
1
1..5
12345
Each step lowers i by one, so one new digit appears on the left of the previous sequence.
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
Outer loop counts down; inner loop appends from i up to rows.
JavaScript
const rows = 5;
for (let i = rows; i >= 1; i--) {
let line = "";
for (let j = i; j <= rows; j++) {
line += j;
}
console.log(line);
}
1. Start at the peak.i = rows so the first row is just 5.
2. Append up to the fixed end. Inner j runs from i to rows — when i = 3, that builds 345.
3. End the row once. Call console.log(line) after the inner loop so digits stay on one line.
Example 2 — User Input Rows
Read rows with prompt and validate before drawing.
JavaScript
const rows = parseInt(prompt("Enter number of rows:"), 10);
if (!Number.isFinite(rows) || rows < 1) {
console.log("Please enter a positive integer.");
} else {
for (let i = rows; i >= 1; i--) {
let line = "";
for (let j = i; j <= rows; j++) {
line += j;
}
console.log(line);
}
}