Shape Rule
Wide first
First line has rows stars; each next line has one fewer down to 1.

The inverted right-angled triangle is the mirror of Program 1: same inner star loop, but the outer loop counts down so the widest line prints first. This tutorial covers reverse iteration, an equivalent forward-loop formula, a live preview, algorithm steps, worked JavaScript examples, edge cases, and complexity.
Wide first
First line has rows stars; each next line has one fewer down to 1.
Countdown
for (let i = rows; i >= 1; i--) starts at the widest row.
Same as P1
for (let j = 1; j <= i; j++) still prints exactly i stars.
rows - i + 1
Forward outer loop with star count rows - i + 1 draws the same shape.
1–20 rows
Pick a row count and draw the inverted triangle instantly.
Complexity
Total stars = n(n+1)/2 - same count as Program 1.
An inverted right-angled triangle shrinks by one * on each new line. With the right angle on the left, the console output looks like an upside-down staircase of stars.
Compared with Program 1, you keep the same j from 1 to i star loop, but run the outer loop from rows down to 1 so the first printed line is the widest.
Reverse outer iteration is a tiny change with a big visual payoff. Pairing it with Program 1 is one of the fastest ways to read nested loops fluently.
i runs rows → 1.
Still print i stars with line += "*".
Countdown i, or forward with rows - i + 1.
Still O(n²) and n(n+1)/2 stars.
In short: for i from rows down to 1, append i stars with line += "*", then call console.log(line).
Given a positive integer rows, print a left-aligned inverted right-angled triangle of * characters with rows lines.
// First 5 rows (conceptual shape)
// *****
// ****
// ***
// **
// * | Item | Type | Description |
|---|---|---|
rows | int | Number of triangle lines (typically ≥ 1). First line has this many stars. |
| Printed output | text | Left-aligned rows of *; line with outer index i has i stars. |
for i from rows down to 1:
for j from 1 to i:
append "*"
console.log(line) | Approach | Idea | Best for |
|---|---|---|
| Countdown outer | i = rows..1, print i stars | Clearest “invert Program 1” story |
| Forward + formula | i = 1..rows, print rows - i + 1 stars | When you prefer ascending counters |
| Goal | Pattern |
|---|---|
| Countdown rows | for (let i = rows; i >= 1; i--) |
Print i stars | for (let j = 1; j <= i; j++) line += "*" |
| End the row | console.log(line) |
| Forward equivalent | for (let j = 1; j <= rows - i + 1; j++) |
| One-line row shortcut | console.log("*".repeat(i)) while counting down |
Same star counts - different outer-loop direction (or bound).
i = 1..rowsStars grow: *, **, ***, …
i = rows..1Stars shrink: *****, ****, …, *
rows - i + 1Ascending i, decreasing star count
name bothCountdown is clearer; formula shows bound flexibility
Reach for the inverted triangle when teaching reverse outer bounds after Program 1.
Natural second lab: flip one loop, keep the rest.
Practice i-- outer loops with a clear visual check.
Rewrite with rows - i + 1 to separate “row number” from “star count.”
Lower halves of diamonds reuse the same countdown idea.
Terminal teaching pattern - not how you build app screens.
Key benefit: one-line change from Program 1 that locks in how outer-loop direction controls the picture.
Choose a row count between 1 and 20 and draw the inverted triangle in the browser.
Three complete JavaScript programs - countdown outer loop, prompt input, and a forward-loop equivalent with "*".repeat(stars). Click View Output to reveal sample console results, or Try it Yourself to run in the browser editor.
Print five rows by counting the outer loop down from 5.
rows = 5 (Countdown)Classic reverse outer loop - the clearest invert of Program 1.
let rows = 5;
for (let i = rows; i >= 1; i--) {
let line = "";
for (let j = 1; j <= i; j++) {
line += "*";
}
console.log(line);
} When i = 5, the inner loop appends five stars. Then i becomes 4, 3, 2, and finally 1 - each time appending fewer stars. console.log(line) after the inner loop starts the next (shorter) row.
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 <= i; j++) {
line += "*";
}
console.log(line);
}
} Same countdown 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 shape without counting the outer loop backward.
rows - i + 1Ascending i with a decreasing star count; uses "*".repeat(stars) for brevity.
let rows = 5;
for (let i = 1; i <= rows; i++) {
const stars = rows - i + 1;
console.log("*".repeat(stars));
} When i = 1, stars = 5; when i = 5, stars = 1. Same picture as the countdown version - useful when an interviewer asks for an ascending outer loop.
Set rows (fixed or from prompt). The first line will have rows stars.
for (let i = rows; i >= 1; i--) starts at the widest row and counts down.
for (let j = 1; j <= i; j++) prints exactly i stars with line += "*".
console.log(line) ends the row before i decreases again.
Total stars: n(n+1)/2 — O(n²) time, O(1) extra memory.
rows = 4Trace each outer-loop value of i as it counts down, and count how many times the inner loop runs.
i | Inner j range | Printed row | Stars this row |
|---|---|---|---|
4 | 1..4 | **** | 4 |
3 | 1..3 | *** | 3 |
2 | 1..2 | ** | 2 |
1 | 1..1 | * | 1 |
Total star prints: 4 + 3 + 2 + 1 = 10 = 4×5/2 - same total as the upright triangle of height 4.
Where this inverted pattern (and reverse outer loops) shows up beyond the homework prompt.
Show how one bound change flips the picture.
Example: side-by-side outputs for rows = 5.
Countdown outer loops appear again in filled diamonds.
Example: Program 10 lower phase.
Practice expressing star count as a formula of i.
Example: stars = rows - i + 1.
Swap * for digits once the countdown works.
Example: print i instead of *.
Same triangular total as the upright triangle.
Example: count stars for n = 10 → 55.
Pair with input validation and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: say “same inner loop as Program 1, outer loop reversed” before coding - that is the whole design.
Why this inverted pattern is a perfect second exercise.
Change one loop header and the picture flips - great for learning.
Wrong outer direction or bound shows up as the upright triangle.
Countdown or forward formula - both are interview-friendly.
Streaming output needs no storage beyond loop counters.
Pro Tip: lead with the countdown story for clarity, then mention the rows - i + 1 rewrite as a follow-up.
Small habits that keep inverted-triangle code clean.
rows, Not a LiteralWrite i = rows so changing the height does not require editing the loop header twice.
Only call console.log(line) after the inner star loop finishes.
parseInt(prompt(), 10) in try/exceptAvoid crashes when the user types letters instead of a number.
Countdown and rows - i + 1 - pick one, mention the other.
Trace rows = 3 on paper before coding larger demos.
Pro Tip: if the output grows like Program 1, your outer loop is still counting up - flip it or switch the star bound.
Mistakes that commonly break inverted star triangles.
You reprint Program 1 instead of the inverted shape.
→ Use for (let i = rows; i >= 1; i--) or change the star bound.
Each star lands on its own line - a column, not a triangle.
→ Use line += "*" for stars; console.log(line) only after the inner loop.
5 in the LoopChanging rows no longer updates the outer bound.
→ Always start from the rows variable.
Using rows - i instead of rows - i + 1 drops the last star on each line.
→ First forward row needs rows stars: rows - 1 + 1.
Letters or empty input throw NaN.
→ Catch NaN and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just * - same as Program 1 for n = 1.
Outer loop never runs - print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
First line has n stars - fine for labs; noisy for huge n.
parseInt(prompt(), 10) yields NaN - validate first.
i == rowsRemember: the first printed line uses the largest i, not index 1.
Try these variations to lock in the pattern.
rows - i + 1 starsrows >= 1n(n+1)/2 - only print order changes.rows > 0 for interactive programs; rows = 1 prints a single star.Quick Takeaway: outer loop counts down from rows, inner loop prints i stars, then break the line - that is the inverted triangle.
| Program | Time | Extra space |
|---|---|---|
| Countdown nested loops (Examples 1–2) | O(rows²) | O(1) |
Forward + "*".repeat (Example 3) | O(rows²) | O(rows) temporary per row string |
The inverted right-angled triangle is Program 1 with a reversed outer loop: widest line first, then one fewer star each row. Master the countdown version, then know the rows - i + 1 rewrite for ascending counters.
Practice the three examples above, then continue to the right-aligned triangle for leading spaces.
Outer loop rows → 1, inner loop prints i stars - keep line += "*" and console.log(line) separated, and validate row counts when reading input.
rows, not a hard-coded numberline += "*" for stars and console.log(line) after each rowrows - i + 1 alternate formulationconsole.log(line) inside the inner star looprows - i when you meant rows - i + 1rows = 1 edge casePrint the upside-down triangle the beginner-friendly way.
Wide line first
Definitionrows → 1
CodeSame as Program 1
Coderows - i + 1
OptionO(n²) time
AnalysisThis inverted triangle uses the same inner loop as Program 1 - only the outer loop direction changes. Total stars stay n(n+1)/2, so complexity is still O(n²).
Add a leading-space loop so the right angle sits on the right edge.
12 people found this page helpful