Shape Rule
Growing reverse rows
Row i prints i letters from top down.

Print a reverse alphabet right-angled triangle: each row has one more character than the previous, and letters go from a top letter down toward A — E, ED, EDC, EDCB, EDCBA. Same geometry as Program 1, but descending along the alphabet. Includes a live preview, worked JavaScript examples, edge cases, and complexity.
Growing reverse rows
Row i prints i letters from top down.
Row length
for (let i = 1; i <= rows; i++) picks how many letters each row prints.
Always from top
for (let code = top; code > top - i; code--) appends descending codes from top.
Letter codes
top = "A".charCodeAt(0) + rows - 1 then String.fromCharCode(code) for output.
1–10 rows
Pick a row count and draw the reverse triangle instantly in the browser.
Complexity
Triangular letter count: n(n+1)/2 prints.
A reverse alphabet right-angled triangle grows like Program 1, but every row starts at a fixed top letter and counts downward until a row-specific end letter.
In JavaScript you solve it with nested for loops and a descending inner counter: the outer loop picks the row length, the inner loop appends letter codes from top down, then console.log(line) moves to the next line.
It locks in reverse iteration with range step -1 — the same skill used in reverse triangles, diagonals, and mirrored alphabet labs.
1, 2, 3, … letters per row.
Inner loop restarts at the top letter.
for (let code = top; code > top - i; code--) counts down.
Same triangle; opposite letter direction.
In short: for each row i from 1 to rows, append top..top-i+1 with line += String.fromCharCode(code), then call console.log(line).
Given a positive integer rows, print a left-aligned reverse alphabet right-angled triangle of letters with rows lines.
# First 5 rows (conceptual shape)
# E
# ED
# EDC
# EDCB
# EDCBA | Item | Type | Description |
|---|---|---|
rows | int | Number of triangle lines to print (typically ≥ 1). |
top | int (code) | Top letter code: "A".charCodeAt(0) + rows - 1. |
| Printed output | text | Growing reverse prefixes from top down to A on the last row. |
top = "A".charCodeAt(0) + rows - 1
for i from 1 to rows:
line = ""
for code from top down to top - i + 1:
line += letter
console.log(line) | Approach | Idea | Best for |
|---|---|---|
code-- | Outer row length + inner descending codes | Learning and interviews |
| Char outer loop | Walk end letter from top down to A | Matching classic E…EDCBA samples |
| Goal | Pattern |
|---|---|
| Top letter | top = "A".charCodeAt(0) + rows - 1 |
| Walk each row | for (let i = 1; i <= rows; i++) |
| Print descending | for (let code = top; code > top - i; code--) line += String.fromCharCode(code) |
| End the row | console.log(line) |
| Forward triangle | See Program 1 |
| Lowercase | Use "a".charCodeAt(0) as the base instead of "A".charCodeAt(0) |
Same triangle idea as Program 1 — only letter direction changes.
letterPrints each descending letter on the current row
breakEnds the row after top..end finishes
top..downInner loop uses code--
A..endInner loop counts up from A
Reach for this when teaching reverse character loops on a growing triangle.
Keep the triangle; flip letter direction to descending.
Practice for (let code = top; code > top - i; code--) and stopping at top - i + 1.
Next you change only the starting letter while counting forward.
Practice top = "A".charCodeAt(0) + rows - 1 for any height.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one bound change (descending code--) turns a forward triangle into a reverse one.
Choose between 1 and 10 rows and draw the reverse alphabet triangle in the browser.
Three complete JavaScript programs - fixed row count, prompt input, and a spaced-letter variant. Click View Output to reveal sample console results, or Try it Yourself to run in the browser editor.
Print five reverse rows with nested loops and a descending inner counter.
rows = 5Hard-coded height - ideal for first demos and screenshots.
const rows = 5;
const top = "A".charCodeAt(0) + rows - 1;
for (let i = 1; i <= rows; i++) {
let line = "";
for (let code = top; code > top - i; code--) {
line += String.fromCharCode(code);
}
console.log(line);
} When i = 1, the inner loop appends E. When i = 3, it appends EDC, and so on through five letters on the last row. console.log(line) after the inner loop starts the next row.
Let the user choose the height at runtime.
Read the row count with prompt() and clamp with Math.max(1, Math.min(rows, 26)). Validate with Number.isFinite in real apps.
let rows = parseInt(prompt("Enter the number of rows:"), 10);
if (!Number.isFinite(rows)) rows = 5;
rows = Math.max(1, Math.min(rows, 26));
const top = "A".charCodeAt(0) + rows - 1;
for (let i = 1; i <= rows; i++) {
let line = "";
for (let code = top; code > top - i; code--) {
line += String.fromCharCode(code);
}
console.log(line);
} Same charCodeAt/fromCharCode core as Example 1; only the source of rows changes. For 4 rows, top becomes "D".charCodeAt(0). Non-numeric input yields NaN with bare parseInt(prompt()) - check Number.isFinite for safer labs.
Same reverse triangle with spaces between letters.
Append a trailing space after each letter so columns are easier to scan.
const rows = 5;
const top = "A".charCodeAt(0) + rows - 1;
for (let i = 1; i <= rows; i++) {
let line = "";
for (let code = top; code > top - i; code--) {
line += String.fromCharCode(code) + " ";
}
console.log(line);
} Loop bounds are unchanged - only the appended unit becomes String.fromCharCode(code) + " ". Trim trailing spaces later if you need a compact line.
Set rows (fixed or from prompt()). Compute top = "A".charCodeAt(0) + rows - 1.
for (let i = 1; i <= rows; i++) selects how many letters print on the current line.
for (let code = top; code > top - i; code--) appends each letter with line += String.fromCharCode(code).
console.log(line) ends the row so the next outer iteration starts fresh.
Total letters: 1+2+…+n = n(n+1)/2 — O(n²) time, O(1) extra memory.
rows = 5Trace each outer value of i and the descending codes the inner loop prints from top = E.
Row i | Inner range | Printed row | Letters this row |
|---|---|---|---|
1 | E..D | E | 1 |
2 | E..C | ED | 2 |
3 | E..B | EDC | 3 |
4 | E..A | EDCB | 4 |
5 | E..A (5 letters) | EDCBA | 5 |
*range stops before the end value, so top - 5 is below A and all five letters print. Total letter prints: 1 + 2 + 3 + 4 + 5 = 15 = 5×6/2.
Where this reverse triangle (and its descending loops) shows up beyond the homework prompt.
Clearest alphabet demo of counting letters downward with code--.
Example: flip bounds to Program 1 and compare.
Same triangle geometry — forward vs reverse fill.
Example: print both side by side for n = 5.
Practice computing top from a row count.
Example: rows 1..10 map to A..J.
Add separators without changing loop structure (Example 3).
Example: print String.fromCharCode(code) + " " for readable columns.
Triangular sums make O(n²) easy to see.
Example: 5 rows print 15 letters total.
Pair the pattern with Number.isFinite and clamp to 26.
Example: reject rows > 26 or clamp it.
Pro Tip: say “always start at top, print down for i letters” before coding — that story prevents wrong inner bounds.
Why this pattern earns a spot right after the forward alphabet triangle.
Wrong direction or bounds show up immediately as a non-reverse triangle.
Same structure; only loop direction and range step flip.
for (let code = top; code > top - i; code--) is reusable in many JavaScript patterns.
Streaming output needs no storage beyond loop counters.
Pro Tip: master Program 1 first; treat this page as the same story with arrows reversed.
Small habits that keep reverse-triangle code clean.
Every row starts from the same top letter; only the count changes with i.
for (let code = top; code > top - i; code--)That triple is what produces E, ED, EDC, …
parseInt(prompt()) with Number.isFiniteAvoid crashes when the user types letters instead of a number.
Beyond Z you need a wrap/stop policy for top.
Trace EDC on paper before coding larger n.
Pro Tip: if every row starts with a different letter and runs forward to E, you wrote Program 3 — not this pattern.
Mistakes that commonly break reverse alphabet triangles.
for (let code = start; code < start + i; code++) prints Program 1 instead.
→ Use for (let code = top; code > top - i; code--).
for (let code = top; code > top - i; code++) with default step +1 produces an empty range.
→ Always pass -1 as the third argument.
Each letter lands on its own line — you get a column, not a triangle.
→ Use line += ... for letters; console.log(line) only after the inner loop.
Non-numeric input yields NaN with bare parseInt(prompt()).
→ Wrap in Number.isFinite and validate range.
Large rows makes top walk past Z.
→ Cap input at 26 or define a wrap policy.
Check these inputs before calling the solution done.
Output is just A on one line.
Through EDCBA.
Top is D → D…DCBA.
Reject, clamp, or wrap — decide explicitly.
parseInt(prompt()) yields NaN — validate first.
Same loops with "a".charCodeAt(0) as the base.
Try these variations to lock in the pattern.
String.fromCharCode(code) + " " (Example 3)*i.n(n+1)/2.top = "A".charCodeAt(0) + rows - 1 to generalize any height.Quick Takeaway: start every row at the top letter, print down for i letters, then break the line — that is the whole triangle.
| Program | Time | Extra space |
|---|---|---|
| Fixed / input (Examples 1–2) | O(rows²) | O(1) |
| Spaced letters (Example 3) | O(rows²) | O(1) |
Row k prints k letters; summing 1..n gives n(n+1)/2 character writes.
The reverse alphabet right-angled triangle is a small nested-loop exercise with lasting payoff: fixed top letter, descending inner walk with code--, and growing row length. Master the classic E…EDCBA sample, then try user input and optional spacing.
Practice the three examples above, then continue to Program 3’s triangle where each row starts one letter earlier but still runs forward.
Compute a top letter, print top..top-i+1 on each row, and break only after the inner loop finishes.
top every rowfor (let code = top; code > top - i; code--) for descending outputtop = "A".charCodeAt(0) + rows - 1parseInt(prompt()) with Number.isFinite and cap at 26for (let code = start; code < start + i; code++) bounds for this pattern-1 step in the inner rangeconsole.log(line) inside the inner letter looprows exceed 26 without a policyPrint the reverse alphabet right-angled triangle the beginner-friendly way.
Growing reverse prefixes
DefinitionInner always starts here
CodeCounts down to top-i
CodeEnds each row
I/OO(n²) time
AnalysisRow i prints i letters from the top letter down. For 5 rows the output is E, ED, EDC, EDCB, EDCBA - the descending mirror of Program 1. Total letters = n(n+1)/2.
Next up: each row starts one letter earlier, but letters still run forward to the top.
12 people found this page helpful