Shape Rule
Base on top
Widest odd star run first; tip star lands on the last line.

An inverted centered pyramid reuses Program 5’s formulas — (rows - i) spaces and (2 * i - 1) stars — but runs the outer loop from rows down to 1 so the widest row prints first. This tutorial covers reverse iteration, a live preview, algorithm steps, worked JavaScript examples, edge cases, and complexity.
Base on top
Widest odd star run first; tip star lands on the last line.
i = rows..1
Reverse the outer loop — that is the only change from Program 5.
spaces + stars
rows - i spaces then 2*i - 1 stars — unchanged bodies.
9, 7, 5…
For five rows, printed star counts fall by two each line.
1–14 rows
Pick a height and draw the inverted pyramid instantly.
n² stars
Same totals as Program 5 — O(n²) time, O(1) extra space.
An inverted center-aligned pyramid starts with the base row and narrows to a single tip star, with leading spaces so each shorter run stays centered.
It is the flip of Program 5: keep the same space and star formulas, reverse only the outer loop. That mirrors how Program 2 inverts Program 1, but with odd-width centering. The same body is the lower half of the filled diamond.
Countdown outer loops are a classic interview tweak. Once you see that inverting a pyramid is “same inners, reverse i,” diamonds and stacked shapes become simple composition.
i from rows down to 1.
rows - i rises as i falls.
2*i - 1 steps down by odds.
Lower half of Program 10’s filled diamond.
In short: for i from rows down to 1, print rows - i spaces, then 2 * i - 1 stars, then a newline.
Given a positive integer rows, print an inverted center-aligned pyramid of * characters with rows lines (widest first).
// First 5 rows (spaces shown as ·)
// *********
// ·*******
// ··*****
// ···***
// ····* | Item | Type | Description |
|---|---|---|
rows | int | Pyramid height (typically ≥ 1). First line width is 2 * rows - 1. |
| Printed output | text | Base-to-tip rows: (rows - i) spaces + (2 * i - 1) stars with countdown i. |
for i from rows down to 1:
for j from 1 to (rows - i):
append " "
for k from 1 to (2 * i - 1):
append "*"
console.log(line) | Approach | Idea | Best for |
|---|---|---|
| Countdown + two inners | Same as Program 5; reverse outer | Learning and interviews |
" ".repeat shortcut | Build padding and stars as strings | Shorter demos after formulas click |
| Goal | Pattern |
|---|---|
| Walk rows base → tip | for (let i = rows; i >= 1; i--) |
| Leading spaces | for (let j = 1; j <= rows - i; j++) line += " " |
| Odd star run | for (let k = 1; k <= 2 * i - 1; k++) line += "*" |
| First-line width | 2 * rows - 1 stars, 0 spaces |
| Flip upright | for (let i = 1; i <= rows; i++) (see Program 5) |
| String shortcut | console.log(" ".repeat(rows - i) + "*".repeat(2 * i - 1)) |
Same space/star formulas — outer-loop direction defines upright vs inverted.
i = 1..rowsUpright pyramid — tip first
i = rows..1Inverted pyramid — base first
i starsInverted left triangle — no centering
+ upperFilled diamond — this page as lower half
Reach for an inverted pyramid when teaching countdown loops after the upright centered pyramid.
Natural “change one loop” follow-up once upright pyramids click.
for (let i = n; i >= 1; i--) is a staple interview warm-up.
Filled diamonds print this shape under the upright pyramid.
Same “invert by reversing i” idea, with centering this time.
Console teaching pattern - not how you build app screens.
Key benefit: proves that flipping a centered pyramid is one outer-loop change — the gateway to stacking diamond halves.
Choose a height between 1 and 14 and draw the inverted centered pyramid in the browser.
Three complete JavaScript programs — countdown nested loops, prompt input, and a " ".repeat / "*".repeat shortcut. Click View Output to reveal sample console results, or Try it Yourself to run in the browser editor.
Print a five-row inverted centered pyramid with classic nested loops.
rows = 5Outer loop counts down; space loop uses rows - i; star loop uses 2 * i - 1.
let rows = 5;
for (let i = rows; i >= 1; i--) {
let line = "";
for (let j = 1; j <= rows - i; j++) {
line += " ";
}
for (let k = 1; k <= 2 * i - 1; k++) {
line += "*";
}
console.log(line);
} When i = 5, print 0 spaces and 9 stars. When i = 1, print 4 spaces and 1 star. Star counts per printed line: 9, 7, 5, 3, 1.
Let the user choose the height at runtime.
Read rows with prompt and parseInt (validate with Number.isFinite in real apps).
let rows = parseInt(prompt("Enter the number of rows:"), 10);
if (!Number.isFinite(rows) || rows < 1) {
console.log("Please enter a whole number of rows >= 1.");
} else {
for (let i = rows; i >= 1; i--) {
let line = "";
for (let j = 1; j <= rows - i; j++) {
line += " ";
}
for (let k = 1; k <= 2 * i - 1; k++) {
line += "*";
}
console.log(line);
}
} Same countdown space/star core as Example 1; only the source of rows changes. Non-numeric input yields NaN with bare parseInt(prompt(), 10) — validate with Number.isFinite for safer labs.
Same inverted pyramid without explicit character loops.
" ".repeat and "*".repeatBuild each row’s margin and odd star run in one call each, still counting down.
let rows = 5;
for (let i = rows; i >= 1; i--) {
console.log(" ".repeat(rows - i) + "*".repeat(2 * i - 1));
} Same formulas as Example 1; " ".repeat and "*".repeat replace the two inner loops. Keep the nested-loop version for exams that want both bounds visible.
Set rows. Use i for the row, j for spaces, k for stars.
for (let i = rows; i >= 1; i--) — base when i == rows, tip when i == 1.
for (let j = 1; j <= rows - i; j++) line += " " — margin grows as i shrinks.
for (let k = 1; k <= 2 * i - 1; k++) line += "*", then console.log(line).
Total stars still n²; O(n²) time, O(1) extra space. First line width 2n - 1.
rows = 4Trace spaces, stars, and characters per row as i counts down from 4 to 1.
i | Spaces rows - i | Stars 2*i - 1 | Chars before newline | Printed row |
|---|---|---|---|---|
4 | 0 | 7 | 7 | ******* |
3 | 1 | 5 | 6 | ***** |
2 | 2 | 3 | 5 | *** |
1 | 3 | 1 | 4 | * |
Star total: 7+5+3+1 = 16 = 4² — same as Program 5, different print order.
Where this inverted pyramid (and countdown centering) shows up beyond the homework prompt.
One formula pair, two shapes — upright vs inverted.
Example: flip Program 5’s outer loop only.
Stack under Program 5 (often from rows - 1).
Example: Program 10.
Change to i = 1..rows to restore Program 5.
Example: Program 5.
Same invert idea; Program 2 has no leading spaces.
Example: side-by-side for rows = 5.
Once solid works, print border stars only.
Example: stars on edges of each odd run.
Pair with input validation and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: say “Program 5 inners, countdown outer” before coding — that is the whole design.
Why the inverted pyramid is a favorite follow-up pattern.
Reuse known space/star formulas; only reverse i.
Wrong direction instantly prints an upright pyramid instead.
Pair with Program 5 for filled diamonds.
Total stars still n² — order does not change big-O.
Pro Tip: master the nested-loop countdown first; treat "*".repeat as a polish shortcut afterward.
Small habits that keep inverted-pyramid code clean.
rows, Step Downi++ by mistake reprints Program 5.
2 * i - 1Even widths break the classic single-peak tip.
Number.isFiniteAvoid NaN rows when the user types letters instead of a number.
Tabs break centering across fonts and editors.
Trace rows = 4 countdown on paper before larger demos.
Pro Tip: if the tip prints first, you almost certainly used i++ instead of i--.
Mistakes that commonly break inverted pyramids.
for (let i = 1; i <= rows; i++) reprints the upright pyramid.
→ Use for (let i = rows; i >= 1; i--).
i Stars Instead of 2*i - 1You lose the centered odd-width shape.
→ Keep odd counts: 2 * i - 1.
j < rows - i instead of <= drops a needed space and shifts the peak.
→ Use j <= rows - i.
Centering looks fine in one editor and broken in another.
→ Always print the space character " ".
Letters or empty input yield NaN.
→ Check with Number.isFinite and re-prompt on failure.
Check these inputs before calling the solution done.
One iteration: 0 spaces + 1 star — tip and base coincide.
Outer loop never runs - print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Top width 2n-1 — fine for labs; may wrap on tiny terminals.
parseInt(prompt(), 10) yields NaN - validate first.
i == rowsSpace loop runs 0 times; print 2*rows-1 stars only.
Try these variations to lock in the pattern.
i = 1..rows2*i-1rows * rowsrows - 1rows + i - 1 — tip rows are shorter than the top base.rows > 0 for interactive programs; rows = 1 prints a single star.Quick Takeaway: countdown i from rows to 1, print rows - i spaces and 2 * i - 1 stars — that is the inverted pyramid.
| Program | Time | Extra space |
|---|---|---|
| Nested space/star loops (Examples 1–2) | O(rows²) | O(1) |
"*".repeat shortcut (Example 3) | O(rows²) | O(rows) temporary per row string |
Total stars = rows²; each row also prints up to Θ(rows) spaces. Same as Program 5.
The inverted centered pyramid is Program 5 with a countdown outer loop: rows - i spaces and 2 * i - 1 stars, printed from base to tip. Master that flip and diamond halves become a short stacking exercise.
Practice the three examples above, then continue to the hollow inverted-V pattern.
Spaces grow, odd stars shrink, total stars = n² — keep i--, and validate row counts when reading input.
Number.isFinite for interactive demosi when you meant an inverted pyramid2 * i even widths for the classic shaperows = 1 edge casePrint the upside-down pyramid the beginner-friendly way.
Countdown + odd stars
Definitioni = rows..1
DirectionSame as Prog 5
Formulan² stars
MathO(n²) time
AnalysisThis inverted pyramid is exactly Program 5 with the outer loop reversed — the same relationship as Program 1 versus Program 2, but with centered odd-width rows. Total stars still equal n²; only print order changes.
Next up: a hollow inverted-V outline that becomes the upper half of a hollow diamond.
12 people found this page helpful