Shape Rule
Centered pyramid
Row 1 prints 1, row 2 prints 2 3 4, row 3 prints 5 6 7 8 9 with leading spaces.

The centered continuous number pyramid prints 1, then 2 3 4, then 5 6 7 8 9 — a natural step after the mirror pattern in Program 23. This tutorial covers odd row widths, leading spaces, a running counter k, a live preview, algorithm steps, worked JavaScript examples, edge cases, and complexity.
Centered pyramid
Row 1 prints 1, row 2 prints 2 3 4, row 3 prints 5 6 7 8 9 with leading spaces.
i += 2
for (let i = 1; i <= maxN; i += 2) sets odd row widths 1, 3, 5.
if j > i
Reverse loop prints spaces first, then k++ for each number slot.
k never resets
k = 1 before the outer loop; k++ continues across rows.
Odd widths 1–9
Pick a max odd width and draw the centered pyramid instantly in the browser.
Complexity
Each row scans maxN columns; total work scales as n².
A centered continuous number pyramid prints numbers that keep counting across rows, with leading spaces to center each row. With max width 5, the output is 1, 2 3 4, 5 6 7 8 9 (spaces shown in the worked examples below).
In JavaScript you use an outer loop with odd widths, a reverse inner loop with an if for spaces vs k++, then console.log(line) ends each row.
It combines spacing logic with a persistent counter — a step up from Program 23’s three inner loops.
i = 1, 3, 5 controls how many numbers print per row.
if j > i prints spaces before numbers.
k++ never resets — numbers flow across rows.
Follow Program 23; continue to Program 25 (bidirectional triangle) next.
In short: for each odd i, scan j from maxN down to 1 — print a space when j > i, else print k then k++, then console.log(line).
Given a positive odd max width (e.g. 5), print a centered pyramid where numbers increase continuously across rows using a counter k.
# maxN = 5 (conceptual shape — dots show spaces)
# ··1·
# ·2·3·4
# 5·6·7·8·9 | Item | Type | Description |
|---|---|---|
maxN | int | Maximum odd row width — inner loop scans j from maxN down to 1. |
i | int | Outer loop — odd row widths 1, 3, 5 via i += 2. |
j | int | Reverse inner loop — spaces when j > i, else print number. |
k | int | Running counter — starts at 1, increments with k++ across all rows. |
k = 1
for i from 1 to maxN step 2:
line = ""
for j from maxN down to 1:
if j > i:
line += " "
else:
line += k; k = k + 1
console.log(line) | Approach | Idea | Best for |
|---|---|---|
| Spacing + counter | 1, 2 3 4, 5 6 7 8 9 | Learning and interviews |
| User-input max | const maxN = parseInt(prompt(...), 10) | Flexible console programs |
| Safe input | Number.isFinite loop + even-width adjustment | Robust user-facing demos |
| Goal | Pattern |
|---|---|
| Walk rows | for (let i = 1; i <= maxN; i += 2) |
| Init counter | k = 1 before the outer loop |
| Scan columns | for (let j = maxN; j >= 1; j--) |
| Space or number | if (j > i) { line += " "; } else { line += k + " "; k++; } |
| End the row | console.log(line) |
| User input | const maxN = parseInt(prompt(...), 10) |
Same centered pyramid — different ways to control width and input validation.
i += 2Odd row widths 1, 3, 5
j > iLeading spaces center each row
k++Numbers continue across rows
if/elseOne inner loop handles space vs number
Reach for this pattern when teaching centering with spaces, persistent counters, and if/else inside nested loops.
Natural follow-up after Program 23 — introduces spacing logic and a running counter.
Outer/inner bound practice with an immediate visual check.
Combine loops with prompt() for a flexible row count.
Compare Program 23 (mirror pattern) and Program 25 (bidirectional 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 an odd max width between 1 and 9 and draw the centered continuous pyramid in the browser.
Three complete JavaScript programs — fixed max width, user input, and safe input with validation. Click View Output to reveal sample console results, or Try it Yourself to run the code live.
Print three rows of the centered pyramid with a running counter.
maxN = 5Hard-coded width — ideal for first demos and screenshots.
const maxN = 5;
let k = 1;
for (let i = 1; i <= maxN; i += 2) {
let line = "";
for (let j = maxN; j >= 1; j--) {
if (j > i) {
line += " ";
} else {
line += k + " ";
k++;
}
}
console.log(line);
} When i = 1, append two spaces then 1 — output 1. When i = 3, append one space then 2 3 4. When i = 5, append 5 6 7 8 9 with no leading spaces. k never resets, so numbers continue across rows.
Read the maximum odd width with prompt() instead of hard-coding 5.
Read maxN with prompt() and parseInt(); adjust even widths to the nearest odd value.
const maxInput = prompt("Enter the maximum odd width:");
let maxN = parseInt(maxInput, 10);
if (maxN % 2 === 0) {
maxN--;
}
if (maxN < 1) {
console.log("Please enter a positive whole number.");
} else {
let k = 1;
for (let i = 1; i <= maxN; i += 2) {
let line = "";
for (let j = maxN; j >= 1; j--) {
if (j > i) {
line += " ";
} else {
line += k + " ";
k++;
}
}
console.log(line);
}
} Same spacing + counter core as Example 1; only the source of maxN changes. The even-width adjustment keeps row sizes odd for a proper pyramid shape. Non-numeric input yields NaN with bare parseInt(prompt(), 10) — validate with Number.isFinite for safer labs.
Use a while loop with Number.isFinite so bad input does not crash the script.
Validate input before drawing the pyramid — prompt again on failure.
let maxN = 0;
while (maxN < 1) {
const maxInput = prompt("Enter the maximum odd width:");
maxN = parseInt(maxInput, 10);
if (!Number.isFinite(maxN) || maxN < 1) {
maxN = 0;
console.log("Please enter a positive whole number.");
}
}
if (maxN % 2 === 0) {
maxN--;
}
let k = 1;
for (let i = 1; i <= maxN; i += 2) {
let line = "";
for (let j = maxN; j >= 1; j--) {
if (j > i) {
line += " ";
} else {
line += k + " ";
k++;
}
}
console.log(line);
} The while (maxN < 1) loop re-prompts when input is not a positive integer — then the pyramid draws as usual.
console.log is built in; use prompt() when reading input. Set k = 1 and loop variables i, j.
for (let i = 1; i <= maxN; i += 2) — row widths 1, 3, 5 grow the pyramid.
for (let j = maxN; j >= 1; j--) scans columns from right to left.
if (j > i) appends a space; else line += k + " " and k++.
console.log(line) ends the row after the inner loop.
Numbers continue across rows — O(n²) time, O(1) extra memory.
maxN = 5Trace each outer-loop value of i, leading spaces, numbers printed, and k after each row.
i | Leading spaces | Numbers printed | k after row | Row output |
|---|---|---|---|---|
1 | 2 (when j = 5, 4) | 1 | 2 | 1 |
3 | 1 (when j = 5) | 2, 3, 4 | 5 | 2 3 4 |
5 | 0 | 5, 6, 7, 8, 9 | 10 | 5 6 7 8 9 |
Leading spaces per row = (max - i) / 2 when maxN is odd — centers each row.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: put console.log(line) inside the inner loop by mistake.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: reset k each row and compare output.
Practice line += vs console.log(line) 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: append k + " " with padded widths for 2-digit numbers.
Triangular totals make O(n²) concrete for beginners.
Example: count printed numbers for maxN = 9 → 1 + 3 + 5 + 7 + 9 = 25.
Pair the pattern with Number.isFinite and positive-width checks.
Example: reject maxN <= 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: trace the reverse inner loop on paper for maxN = 3 before coding — spacing bugs hide in the j > i condition.
Small habits that keep number-pattern code clean.
Do not reset k inside the outer loop unless you want per-row numbering.
prompt()Validate parseInt(prompt(), 10) with Number.isFinite so bad input does not produce NaN.
console.log(line) OutsideOnly call console.log(line) after the inner loop finishes the row.
Write each i, space count, and numbers printed before coding.
Trace maxN = 3 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 centered pyramid patterns.
Each digit lands on its own line — you get a column, not a triangle.
→ Use line += " " or line += k + " "; k++; console.log(line) only after the inner loop.
Putting k = 1 inside the outer loop restarts numbering — you lose the continuous effect.
→ Initialize k = 1 once before the outer loop unless you want per-row numbering.
Without if j > i the pyramid is left-aligned, not centered.
→ Print a space when j > i before printing numbers.
Even maxN values break the centering math for this version.
→ Subtract 1 when maxN % 2 == 0, or validate and prompt again.
Letters or empty input yield NaN from bare parseInt(prompt(), 10).
→ Validate with Number.isFinite and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just a centered 1 with leading spaces.
Outer loop never runs — print nothing or show a message.
maxN < 0Treat as invalid; re-prompt instead of silent empty output.
Subtract 1 to force odd width, or re-prompt for an odd value.
parseInt(prompt(), 10) yields NaN — validate with Number.isFinite first.
Two rows: centered 1 and 2 3.
Try these variations to lock in the pattern.
k = 1 inside the outer loopk++ on charsj > i shift numbers right — row width stays at maxN columns.line += stays on the line; console.log(line) advances — mix them carefully.maxN > 0 for interactive programs; maxN = 1 prints a single centered 1.Quick Takeaway: odd outer loop (i += 2), reverse inner loop with if j > i, persistent k++, then console.log(line) after each row.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(n²) | O(1) |
| Safe input (Example 3) | O(n²) | O(1) |
The centered continuous number pyramid is a compact lesson in spacing and counters: print leading spaces when j > i, then k++ for each number slot. Master the fixed-maxN version, then try user input and safe prompt() validation.
Practice the three examples above, then continue to Program 25 for the bidirectional number triangle.
Never reset k inside the outer loop unless you want per-row numbering — validate maxN when reading from the console.
for (let i = 1; i <= maxN; i += 2) in the outer loopk = 1 before the outer loopj > i, else print k and k++parseInt(prompt(), 10) with Number.isFinite before using maxNconsole.log(line) inside the inner loopk inside the outer loop (unless intentional)maxN without adjustmentmaxN = 1 edge casePrint the pattern the beginner-friendly way.
Spaces + k++
DefinitionOdd widths
CodeCentering
CodeNever reset
ShapeO(n²) time
AnalysisThis centered pyramid prints numbers continuously using a counter k. An if inside a reverse loop appends leading spaces when j > i, then appends k and does k++ once the column reaches the row boundary.
Move on to the bidirectional number triangle in the JavaScript number-pattern series.
12 people found this page helpful