Shape Rule
Repeat letter, grow width
Row 1 prints E, row 2 prints DD, up to AAAAA on the last line.

Same repeating idea as Program 9, but letters run E → D → C → B → A while row widths still grow 1, 2, 3, 4, 5. This tutorial covers the shape rule, countdown loops, a live preview, worked JavaScript examples, edge cases, and complexity.
Repeat letter, grow width
Row 1 prints E, row 2 prints DD, up to AAAAA on the last line.
Letter countdown
for (let i = "E".charCodeAt(0); i >= "A".charCodeAt(0); i--) picks the letter for each row.
Repeat count
for (let j = "E".charCodeAt(0); j >= i; j--) runs 1, 2, 3… times - print String.fromCharCode(i), not the inner counter.
Same line / next line
Letters use line += ch; end each row with console.log(line).
1–26 rows
Pick a row count and draw the reverse repeating triangle in the browser.
Complexity
Total letters = n(n+1)/2; extra memory stays O(1).
A reverse repeating alphabet triangle grows by one repeated letter on each new line, counting letters downward from the top of the alphabet range. With the right angle on the left, the console shows a staircase of identical letters per row.
In JavaScript you usually solve it with two nested for loops: the outer loop picks the row letter (counting down), the inner loop prints that same letter the right number of times, then console.log(line) moves to the next line.
It locks in the difference between “which letter” (outer loop) and “how many times” (inner loop). Once that clicks, forward repeats, pyramids, and letter-countdown variants become much easier.
Rows use E, then D, then C … down to A.
Row widths are still 1, 2, 3, … like Program 9.
line += String.fromCharCode(i) in the inner loop; console.log(line) after.
Same shape - reverse letter direction only.
In short: for each letter i from top down to A, print i repeatedly (growing count), then call console.log(line).
Given a positive integer rows (or a fixed top letter like 'E'), print a left-aligned triangle where each row repeats one letter and letters count downward.
// First 5 rows (conceptual shape)
// E
// DD
// CCC
// BBBB
// AAAAA | Item | Type | Description |
|---|---|---|
rows | int | Number of triangle lines (typically 1–26). Top letter = String.fromCharCode("A".charCodeAt(0) + rows - 1). |
| Printed output | text | Left-aligned rows; row k repeats letter top - (k-1) exactly k times. |
top = "A".charCodeAt(0) + rows - 1
for code from top down to "A".charCodeAt(0):
repeat = (top - code) + 1
for k from 1 to repeat:
print String.fromCharCode(code) (no newline)
console.log the row | Approach | Idea | Best for |
|---|---|---|
| Nested char loops | Outer letter + inner count via char range | Learning and interviews |
ch.repeat(repeat) | Build a whole row in one call | Shorter production-style demos |
| Goal | Pattern |
|---|---|
| Walk letters downward | for (let i = "E".charCodeAt(0); i >= "A".charCodeAt(0); i--) |
| Grow repeat count | for (let j = "E".charCodeAt(0); j >= i; j--) |
| Print row letter | line += String.fromCharCode(i) — not the inner counter |
| End the row | console.log(line) |
| One-line row shortcut | console.log(ch.repeat(repeat)) |
| Forward letters | See Program 9 (A, BB, CCC, …) |
Same triangle - different ways to emit characters.
same linePrints a letter without moving to the next line
new lineEnds the current row after all repeats are printed
whole rowBuilds n copies of ch at once - skip the inner loop
print String.fromCharCode(i)Master printing the outer letter before the string shortcut
Reach for this triangle when practicing letter direction vs repeat count.
Flip letter direction while keeping the same growing widths.
Clear drill: outer = which letter, inner = how many copies.
Compute top = "A".charCodeAt(0) + rows - 1 and count down safely.
Lowercase, hollow borders, or centered pyramids next.
This is a console teaching pattern - not how you build modern app screens.
Key benefit: one small program that separates letter choice from repeat count - the skill behind most alphabet patterns.
Choose a row count between 1 and 26 and draw the reverse repeating alphabet triangle in the browser.
Three complete JavaScript programs - fixed E-to-A range, prompt input, and a ch.repeat(repeat) shortcut. Click View Output to reveal sample console results, or Try it Yourself to run in the browser editor.
Print five rows with classic nested char loops.
'E' down to 'A'Hard-coded range - ideal for first demos and screenshots.
const top = "E".charCodeAt(0);
const base = "A".charCodeAt(0);
for (let i = top; i >= base; i--) {
const ch = String.fromCharCode(i);
let line = "";
for (let j = top; j >= i; j--) {
line += ch;
}
console.log(line);
} When i is "E".charCodeAt(0), the inner loop runs once and builds E. When i is "D".charCodeAt(0), it builds DD, and so on until A. Appending ch from the outer letter (not the inner counter) keeps each row uniform.
Let the user choose the height at runtime.
Compute top = "A".charCodeAt(0) + rows - 1, then count down. Validate parseInt(prompt()) with Number.isFinite in real apps.
const rowsInput = prompt("Enter the number of rows:");
let rows = parseInt(rowsInput, 10);
rows = Math.max(1, Math.min(rows, 26));
const base = "A".charCodeAt(0);
const top = base + rows - 1;
for (let code = top; code >= base; code--) {
const ch = String.fromCharCode(code);
const repeat = top - code + 1;
let line = "";
for (let k = 0; k < repeat; k++) {
line += ch;
}
console.log(line);
} For rows = 4, top is "D".charCodeAt(0). Each step down increases repeat by one. Clamp rows to 1-26 so top stays within A-Z.
Same shape without an explicit inner append loop.
ch.repeat(repeat)Build each repeated-letter row in one call, then log it.
const rows = 5;
const base = "A".charCodeAt(0);
const top = base + rows - 1;
for (let code = top; code >= base; code--) {
const ch = String.fromCharCode(code);
const repeat = top - code + 1;
console.log(ch.repeat(repeat));
} ch.repeat(repeat) creates a string of length repeat filled with that letter. Great once you understand the nested-loop idea; keep the two-loop version for exams that ask you to show both bounds.
Use prompt() when reading input. Fix the top letter or compute it from rows.
for (let i = "E".charCodeAt(0); i >= "A".charCodeAt(0); i--) selects the character printed on the row.
for (let j = "E".charCodeAt(0); j >= i; j--) runs 1, 2, 3… times; print String.fromCharCode(i) with line += String.fromCharCode(i).
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.
'E' down to 'A'Trace each outer-loop value of i and count how many times the inner loop runs.
i | Inner j range | Printed row | Repeats |
|---|---|---|---|
'E' | 'E'..'E' | E | 1 |
'D' | 'E'..'D' | DD | 2 |
'C' | 'E'..'C' | CCC | 3 |
'B' | 'E'..'B' | BBBB | 4 |
'A' | 'E'..'A' | AAAAA | 5 |
Total letter prints: 1 + 2 + 3 + 4 + 5 = 15 = 5×6/2.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Best demo that the printed value need not be the loop counter.
Example: swap line += String.fromCharCode(i) for line += String.fromCharCode(j) and watch letters step.
Teach direction as a one-line change: increment vs decrement.
Example: side-by-side A/BB/CCC vs E/DD/CCC.
Practice "A".charCodeAt(0) + rows - 1 without complex algorithms.
Example: rows = 7 → top = 'G'.
Swap to lowercase or mix digits once the loops work.
Example: start from 'a' + rows - 1.
Triangular totals make O(n²) concrete for beginners.
Example: count printed letters for n = 10 → 55.
Pair the pattern with Number.isFinite and 1–26 clamps.
Example: reject rows <= 0 or rows > 26.
Pro Tip: when explaining this pattern, say “outer picks the letter, inner only counts” before writing any code - that story prevents the line += String.fromCharCode(j) mistake.
Why this pattern earns a spot right after the forward repeating triangle.
Wrong printed variable shows up immediately as stepping letters.
Only loops, chars, and console output - no arrays required.
Flip to Program 9 by counting letters upward instead.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the nested-loop version first; treat ch.repeat(repeat) as a polish shortcut afterward.
Small habits that keep alphabet-pattern code clean.
Use ch for the row letter and repeat (or k) for the count - clearer than overloaded i/j.
Avoid crashes when the user types letters instead of a number.
Only call console.log(line) after the inner loop finishes the row.
For A–Z demos, reject or clamp rows > 26.
Trace rows = 3 (C, BB, AAA) on paper before coding larger demos.
Pro Tip: if a row shows EDCBA-style sequences, you almost certainly printed j instead of i.
Mistakes that commonly break reverse repeating alphabet patterns.
j Instead of iRows become countdown sequences instead of repeated letters.
→ Always line += String.fromCharCode(i) (or ch) for this shape.
Each letter lands on its own line - you get a column, not a triangle.
→ Use line += ch for letters; console.log(line) only after the inner loop.
Omitting console.log(line) after the inner loop glues every letter onto one endless line.
→ Always end the row after the inner loop.
Non-numeric input yields NaN with bare parseInt(prompt()).
→ Check with Number.isFinite and validate range.
"A".charCodeAt(0) + rows - 1 can leave the A–Z range.
→ Clamp to 26 or define wrap/error behavior explicitly.
Check these inputs before calling the solution done.
Output is just A on one line.
Treat as invalid; re-prompt instead of silent empty output.
rows < 0Invalid height - validate before computing top.
Clamp or error - char math leaves A–Z.
parseInt(prompt()) yields NaN - validate first.
Same loops work with 'a' and 'a' + rows - 1.
Try these variations to lock in the pattern.
'a' + rows - 1 as the top letterNumber.isFinite until 1 <= rows <= 26n(n+1)/2 - hence O(n²) time.1 <= rows <= 26 for interactive A–Z programs.Quick Takeaway: outer loop picks the letter (counting down), inner loop repeats it, then break the line - that is the whole pattern.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1-2) | O(rows²) | O(1) |
ch.repeat(repeat) (Example 3) | O(rows²) | O(rows) per row string (temporary) |
The reverse repeating alphabet triangle is a small nested-loop exercise with lasting payoff: outer letter vs inner count, char countdown, and O(n²) intuition. Master the classic two-loop version, then optionally shorten rows with ch.repeat(repeat).
Practice the three examples above, then compare with Program 9 or continue to the next alphabet pattern.
Print the outer letter with line += ch, end rows with console.log(line), and clamp row counts to 1–26 when reading input.
line += String.fromCharCode(i) for letters and console.log(line) after each row1 <= rows <= 26 for interactive programsparseInt(prompt()) with Number.isFiniteconsole.log(line) inside the inner letter looprows > 26 without a clear policyPrint the reverse repeating triangle the beginner-friendly way.
Letters down, width up
DefinitionPicks the row letter
CodeRepeats with line += String.fromCharCode(i)
Ends each row
I/OO(n²) time
AnalysisThis pattern is the reverse of Program 9: row widths still grow 1, 2, 3, …, but letters run backward (E, then D, then C, …). Append the outer loop letter inside the inner loop so each row stays uniform.
Keep building letter patterns with nested loops and char math.
12 people found this page helpful