A descending number triangle prints digits 1..i on each row, with the outer loop counting down so the first row is longest and each next row shortens by one digit.
Remember
Rule: for i = n down to 1
print j from 1 to i
12345
1234
123
12
1 ← n = 5
Classic first console pattern in JavaScript labs — once nested loops and line += j / console.log(line) click, other number triangles get easier.
Approach
How to Solve It
Count the row width down from n; on each row, append digits 1..i, then log the line.
Method
Idea
Best for
Nested loops
Outer i = n..1; build line with += j
Learning, interviews
prompt input
Same loops; read n at runtime
Interactive practice
document.write
Print digits + <br> in the browser
Quick HTML demos
Pseudocode
Pseudocode
for i from n down to 1:
line = ""
for j from 1 to i:
line += j
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 += j;
End row
console.log(line);
Digits on row i
Exactly i
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += j
Stays on the same row
Each 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 number 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
12345
1234
123
12
1
Trace
Worked Walkthrough — n = 3
Trace each outer value of i and the digits printed on that row.
i
Inner j
Printed row
3
1 2 3
123
2
1 2
12
1
1
1
Each step lowers i by 1, so the row loses its last digit.
Code
JavaScript Programs
Three complete programs: fixed n = 5, prompt input, and a browser document.write version. Use View Output for sample results, or Try It Yourself to edit and run in the playground.
1. Outer loop.i starts at n and counts down to 1 — row width shrinks each time.
2. Inner loop. Append j from 1 to i onto line — first row is 12345, then 1234, and so on.
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 += j;
}
console.log(line);
}
}
1. Same bounds. Outer n..1, inner 1..i — identical to Example 1.
2. Write digits.document.write(j) prints each digit into the page.
3. Break the row.document.write("<br>") after the inner loop starts a new 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.
Outer ascending
Wrong order
Counting i up from 1 prints the shortest row first — an ascending triangle, not this pattern.
Reuse line
Growing leftovers
Reset line = "" at the start of each outer iteration, or digits from previous rows stick around.
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
document.write (Example 3)
O(n²)
O(1) beyond the page itself
Total digits printed: 1 + 2 + … + n = n(n+1)/2 → still O(n²).
Remember
Key Takeaways
Rule: outer i = n..1; append j = 1..i on each row.
Shrink: first row is longest; each next row drops one digit.
Write vs log:line += j builds; console.log(line) breaks.
Complexity:O(n²) for n rows.
One line: start at width n and walk down — each row prints 1 through the current width.
Frequently Asked Questions
The outer loop runs i from n down to 1. For each row i, the inner loop runs j from 1 to i and appends j to a line string. The first row prints 1..n; each next row prints one fewer digit.
Counting down makes the first row the longest. for (let i = n; i >= 1; i--) sets i to the full width first, then shrinks by one each line.
line += j stays on the same row while building digits. console.log(line) prints the completed row and adds a newline.
Change the outer loop to for (let i = 1; i <= n; i++). Keep the inner loop as j = 1..i. The first row then has one digit and the last row has n digits.
O(n²) where n is the number of rows. Total digit appends equal 1+2+…+n = n(n+1)/2.
Yes in browser demos. document.write(j) prints each digit and document.write("<br>") ends the row. Prefer console.log or DOM updates in production code.
Use parseInt(prompt(...), 10) and check Number.isFinite(rows) && rows >= 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 i prints digits 1 through i. The outer loop counts down from n, so the first line is longest and each row shortens by one digit — still O(n²) total prints.