A fill-with-n triangle prints ascending digits i..n on each row, then pads with n so every line has the same width — 5 5 5 5 5, 4 5 5 5 5, …
Remember
Rule: for i = n down to 1
print i..n, then pad with n
(i − 1) times
5 5 5 5 5
4 5 5 5 5
3 4 5 5 5
2 3 4 5 5
1 2 3 4 5 ← n = 5
Two inner loops keep every row width n — a clear follow-up after Program 18’s growing rows.
Approach
How to Solve It
Count i down from n; print the sequence i..n, then fill remaining slots with n.
Method
Idea
Best for
Two inner loops
Sequence i..n, then pad with n
Learning, interviews
prompt input
Same loops; read width at runtime
Interactive practice
Custom fill
Pad with a separate fill constant
Variant practice
Pseudocode
Pseudocode
for i from n down to 1:
line = ""
for j from i to n:
line += j + " "
for j from 1 to i − 1:
line += n + " "
print line (with newline)
Cheat sheet
Goal
Pattern
Set width
const n = 5;
Outer loop
for (let i = n; i >= 1; i--)
Sequence
for (let j = i; j <= n; j++) line += j + " ";
Fill pad
for (let j = 1; j < i; j++) line += n + " ";
End row
console.log(line);
Custom fill
line += fill + " "; in the pad loop
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += … + " "
Stays on the same row
Each spaced number
console.log(line)
Ends the line
After both inner loops
Build the row with +=, then break once with console.log. Putting console.log inside an inner loop prints one number per line.
Try it
Live Preview
Change width n and the fill-with-n 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 · numbers = 25
5 5 5 5 5
4 5 5 5 5
3 4 5 5 5
2 3 4 5 5
1 2 3 4 5
Trace
Worked Walkthrough — n = 3
Trace each outer value of i, the sequence, and how many pads are added.
i
Sequence i..n
Pads
Printed row
3
3
2× 3
3 3 3
2
2 3
1× 3
2 3 3
1
1 2 3
none
1 2 3
As i drops, the sequence grows and the pad count i − 1 shrinks — every row stays width n.
Code
JavaScript Programs
Three complete programs: fixed n = 5, prompt input, and a custom fill constant. Use View Output for sample results, or Try It Yourself to edit and run in the playground.
Example 1 — Fixed n = 5
Hard-coded width — sequence loop then fill loop; every row has five numbers.
JavaScript
const n = 5;
for (let i = n; i >= 1; i--) {
let line = "";
for (let j = i; j <= n; j++) {
line += j + " ";
}
for (let j = 1; j < i; j++) {
line += n + " ";
}
console.log(line);
}
1. Outer loop.i runs from n down to 1 — the first number of the sequence.
2. Sequence loop. Append j from i to n with a trailing space.
3. Fill loop. Append n exactly i − 1 times so the row width stays n; then console.log(line).
Example 2 — prompt Input
Read width at runtime; fill value matches n.
JavaScript
const n = parseInt(prompt("Enter the triangle width:"), 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 = i; j <= n; j++) {
line += j + " ";
}
for (let j = 1; j < i; j++) {
line += n + " ";
}
console.log(line);
}
}
1. Prompt and parse. Convert the answer with parseInt(..., 10).
2. Validate first. Reject NaN or non-positive values before looping.
3. Same loops. Sequence end bound and fill value both use the same n.
Example 3 — Custom Fill Constant
Pad with fill = 9 while the sequence still runs up to n = 5.
JavaScript
const n = 5;
const fill = 9;
for (let i = n; i >= 1; i--) {
let line = "";
for (let j = i; j <= n; j++) {
line += j + " ";
}
for (let j = 1; j < i; j++) {
line += fill + " ";
}
console.log(line);
}
One line: start the sequence later on each upper row, and pad the rest with n so every line is the same width.
Frequently Asked Questions
When i = 5, the sequence loop appends j = 5 once, then the fill loop runs 4 times — all values are 5, so the row is 5 5 5 5 5.
The first inner loop appends the increasing sequence i..n. The second fills remaining positions with n so every row has the same width.
When i = 1, the sequence loop appends j = 1 to 5 and the fill loop runs zero times — no padding needed.
line += j + " " stays on the same row with a trailing space. console.log(line) prints the completed row and adds a newline.
Replace n in the second loop with a separate fill constant (see Example 3), or keep n so fill matches the width.
O(n²) for width n because each of n rows appends n numbers.
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?
Each row prints an ascending sequence i..n, then pads with n so every row has width n. The second inner loop runs i - 1 times — still O(n²) total appends.