Shape Rule
Sequence + fill
Row 1 prints 5 5 5 5 5, row 2 prints 4 5 5 5 5, row 3 prints 3 4 5 5 5, and so on.

The fill-with-5 number triangle pads each row with the maximum value so every line has width n — a natural step after alternating odd/even patterns. This tutorial covers the shape rule, two inner loops, a live preview, algorithm steps, worked JavaScript examples, edge cases, and complexity.
Sequence + fill
Row 1 prints 5 5 5 5 5, row 2 prints 4 5 5 5 5, row 3 prints 3 4 5 5 5, and so on.
n..1
for (let i = n; i >= 1; i--) walks rows from the top (all fill) down to the full sequence.
i..n then pad
for (let j = i; j <= n; j++) appends the sequence; for (let j = 1; j < i; j++) fills with n.
Same line / next line
Numbers use line += j + " "; or line += n + " ";; end each row with console.log(line).
1–15 width
Pick a triangle width and draw the fill-with-n pattern instantly in the browser.
Complexity
Each of n rows prints n numbers — total appends = n²; extra memory stays O(1).
A fill-with-5 number triangle prints an ascending sequence on each row, then pads the rest with the maximum value so every row has the same width. With n = 5, the output is 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.
In JavaScript you use a descending outer loop, append j from i to n in the first inner loop, fill remaining slots with n in the second inner loop, then console.log(line) ends each row.
It combines two inner loops with fixed row width — a step up from Program 18.
for (let j = i; j <= n; j++) prints ascending numbers.
for (let j = 1; j < i; j++) pads with n.
line += j + " "; or line += n + " "; in inner loops; console.log(line) after.
Follow Program 18; continue to Program 20 (continuous number triangle).
In short: for each i from n down to 1, append j from i to n, fill i - 1 times with n, then call console.log(line).
Given a positive integer n, print a fill-with-n triangle: each row prints an ascending sequence from i to n, then pads with n so every row has width n.
# n = 5 (conceptual shape)
# 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
for (let i = n; i >= 1; i--)
for (let j = i; j <= n; j++)
line += j + " " # sequence i..n
for (let j = 1; j < i; j++)
line += n + " " # pad with n
console.log(line) | Item | Type | Description |
|---|---|---|
n | int | Triangle width and fill value (typically ≥ 1). |
| Printed output | text | Each row has n spaced numbers — sequence then padding. |
for i from n down to 1:
for j from i to n:
append j + space to line
for j from 1 to i - 1:
append n + space to line
console.log(line) | Approach | Idea | Best for |
|---|---|---|
| Two inner loops | 5 5 5 5 5, 4 5 5 5 5, … | Learning and interviews |
Variable n | parseInt(prompt(), 10) | User-input version |
| Custom fill | Separate fill constant | Pad with a value other than n |
| Goal | Pattern |
|---|---|
| Walk each row | for (let i = n; i >= 1; i--) |
| Print sequence | for (let j = i; j <= n; j++) { line += j + " "; } |
| Fill padding | for (let j = 1; j < i; j++) { line += n + " "; } |
| End the row | console.log(line) |
| User input | parseInt(prompt(), 10) |
| Custom fill value | line += fill + " "; in second loop |
Same fill-with-n triangle — different ways to structure the padding.
j = i..nFirst inner loop prints ascending numbers
pad nSecond loop runs i - 1 times with n
inputReplace hard-coded 5 with user input in Example 2
width nEvery row must print exactly n numbers
Reach for this pattern when teaching two inner loops and fixed-width row padding.
Natural follow-up after Program 18 — combines sequence printing with right padding.
Outer/inner bound practice with an immediate visual check.
Combine loops with prompt() for a flexible row count.
Compare Program 18 (alternating odd/even) and Program 20 (continuous counter triangle) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.
Choose a triangle width between 1 and 15 and draw the fill-with-n pattern in the browser.
Three complete JavaScript programs — fixed width, user input, and custom fill constant. Click View Output to reveal sample results, or Try it Yourself to run the code live.
Print five rows of the fill-with-5 triangle with two inner loops.
n = 5Hard-coded width — ideal for first demos and screenshots.
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);
} When i = 5, the sequence loop prints 5 once, then the fill loop runs 4 times — all 5s. When i = 3, the sequence prints 3 4 5, then two 5s pad the row. When i = 1, the sequence prints 1 2 3 4 5 with no fill needed. console.log(line) after both inner loops starts the next row.
Read the triangle width with prompt() instead of hard-coding 5.
Read n with prompt() and parseInt() (check Number.isFinite in real apps); the fill value matches the width.
const nInput = prompt("Enter the triangle width:");
const n = parseInt(nInput, 10);
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);
} Same nested-loop core as Example 1; only the source of n changes. Both the sequence end bound and the fill value use the same variable. Non-numeric input yields NaN with bare parseInt(prompt()) — validate with Number.isFinite for safer labs.
Use a separate fill constant instead of always padding with n.
Pad with fill = 9 while the sequence still runs up to n = 5.
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);
} Replace n with fill in the second inner loop only. The sequence loop still prints j from i to n; padding uses the custom constant.
print is built in; use prompt() when reading input. Set n (fixed or from input).
for (let i = n; i >= 1; i--) walks from the all-fill top row down to the full sequence.
Print j from i to n, then pad i - 1 times with n (or a custom fill value).
console.log(line) ends the row so the next outer iteration starts fresh.
Total appends: n² — O(n²) time, O(1) extra memory.
n = 5Trace each outer-loop value of i, the sequence printed, the fill count, and the final row.
i | Sequence (j = i..n) | Fill count (i - 1) | Row output |
|---|---|---|---|
5 | 5 | 4 | 5 5 5 5 5 |
4 | 4, 5 | 3 | 4 5 5 5 5 |
3 | 3, 4, 5 | 2 | 3 4 5 5 5 |
2 | 2, 3, 4, 5 | 1 | 2 3 4 5 5 |
1 | 1, 2, 3, 4, 5 | 0 | 1 2 3 4 5 |
Total number prints: 5 + 5 + 5 + 5 + 5 = 25 = 5².
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: change for (let j = 1; j <= i; j++) and watch the shape change.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: use (i + j) % 2 for row+column parity grids.
Practice line += j + " " vs row newline without complex math.
Example: put console.log(line) inside the inner loop by mistake.
Swap digits for letters, stars, or spaced output once the loop works.
Example: use line += j + " "; for spaced numbers on each row.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for n = 10 still → 55.
Pair the pattern with Number.isFinite around parseInt(prompt(), 10) and positive-width checks.
Example: reject n <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner JavaScript courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the sequence loop first, then add the fill loop — compare with custom fill in Example 3.
Small habits that keep number-pattern code clean.
Use n for both width and fill value unless you need a custom constant.
Number.isFiniteCheck Number.isFinite(n) after parseInt(prompt(), 10) so bad input does not leave n unset.
Only call console.log(line) after the inner loop finishes the row.
Write row i, sequence j = i..n, and fill count i - 1 before coding.
Trace n = 5 on paper before coding larger demos.
Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put console.log(line) inside the inner loop.
Mistakes that commonly break fill-with-n number patterns.
Each digit lands on its own line — you get a column, not a triangle.
→ Use line += j + " "; or line += n + " ";; console.log(line) only after both inner loops.
Rows have different widths — the top row may be short while the bottom is full.
→ Add for (let j = 1; j < i; j++) to pad with n after the sequence loop.
for (let j = 1; j <= i; j++) in the fill loop prints too many padding values.
→ Use for (let j = 1; j < i; j++) so the fill runs exactly i - 1 times.
Omitting console.log(line) glues every number onto one endless line.
→ Always end the row after both inner loops.
Letters or empty input return NaN with bare parseInt(prompt(), 10).
→ Check Number.isFinite(n) and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 on one line — no fill needed.
Outer loop never runs — print nothing or show a message.
n < 0Treat as invalid; re-prompt instead of silent empty output.
Output grows as n² characters — fine for labs, noisy for huge n.
parseInt(prompt(), 10) returns NaN on bad input — validate with Number.isFinite first.
Sequence must run for (let j = i; j <= n; j++), not for (let j = 1; j <= i; j++).
Without the fill loop, top rows are shorter than the bottom row.
Try these variations to lock in the pattern.
i % 2 and k += 2nn² — each of n rows prints n numbers.line += j + " "; stays on the line; console.log(line) advances — mix them carefully.n > 0 for interactive programs; n = 1 should print a single 1.Quick Takeaway: descending outer loop, print sequence j = i..n, fill i - 1 times with n, then break the line.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(n²) | O(1) |
| Custom fill (Example 3) | O(n²) | O(1) |
The fill-with-5 number triangle is a compact lesson in two inner loops: the first prints an ascending sequence, the second pads with the maximum value so every row has width n. Master the fixed-n version, then try user input and a custom fill constant.
Practice the three examples above, then continue to Program 20 for the continuous number triangle.
Run the sequence loop first, then the fill loop — use j < i for padding and validate n when reading input.
line += j + " "; and line += n + " ";n ≥ 1 for interactive programsNumber.isFinite(n) after parseInt(prompt(), 10) before using nconsole.log(line) inside the inner digit loopfor (let j = 1; j <= i; j++) in the fill loopn = 1 edge casePrint the pattern the beginner-friendly way.
Sequence then fill
Definitioni = n..1
j = i..n
Pad i - 1 times
O(n²) time
AnalysisEach 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.
Move on to the continuous number triangle in the JavaScript number-pattern series.
12 people found this page helpful