Widening Rule
letter + gap + letter
Each row prints one letter, then for r > 0 a gap of 2*r - 1 spaces and the same letter again.

Each row mirrors the same letter with a growing gap: row 0 prints centered A, row 1 prints B B, row 2 prints C C, until row 4 shows E E for five rows. Leading spaces (rows - 1 - r) center the triangle; the gap formula 2*r - 1 widens each row. Compare with Program 32 (centered palindrome pyramid) and Program 19 (mirrored halves). Includes a live preview, worked JavaScript examples, edge cases, and complexity.
letter + gap + letter
Each row prints one letter, then for r > 0 a gap of 2*r - 1 spaces and the same letter again.
rows - 1 - r
line += " ".repeat(rows - 1 - r) centers each row under the single apex A.
String.fromCharCode(base + r)
ch = String.fromCharCode(base + r) picks the row letter - A on row 0, B on row 1, and so on.
2*r - 1
line += " ".repeat(2 * r - 1) widens the gap between mirrored letters each row.
1–26 rows
Pick a row count and draw the widening alphabet triangle in the browser instantly.
Complexity
Row r prints 2*r - 1 gap spaces when r > 0; total output ≈ O(n²); extra memory stays O(1).
A widening alphabet triangle prints each row as the same letter twice with a growing space gap, padded with leading spaces so the shape is centered. Row 0 prints a single A; each next row steps to the next letter - B B, C C, D D, and so on.
In JavaScript you solve it with an outer loop over row index r, leading spaces via " ".repeat(...), the row letter from charCodeAt(0)/String.fromCharCode(), and for r > 0 a widening gap before the mirrored letter - or build the whole row as one string for clarity.
It combines three classic pattern skills - centering with spaces, conditional row logic, and a widening gap formula - the same building blocks used in hollow pyramids, diamonds, and symmetric ASCII art. Compare with Program 32 to see palindrome rows vs mirrored same-letter pairs.
" " * (rows - 1 - r) - row 0 gets rows - 1 spaces; bottom row gets none.
ch = String.fromCharCode(base + r) - prints the row letter once before the gap.
if r > 0: then gap spaces and the same ch again - skipped on row 0.
if r > 0 ensures row 0 prints only one A, not A A.
In short: set base = "A".charCodeAt(0), loop r from 0 to rows - 1, append (rows - 1 - r) spaces, append ch, if r > 0 append (2*r - 1) gap spaces and ch again, then console.log(line) for the newline.
Given a positive integer rows, print a centered triangle of rows lines. Row r prints (rows - 1 - r) leading spaces, then letter String.fromCharCode("A".charCodeAt(0) + r). For r > 0, print (2*r - 1) gap spaces and the same letter again.
// First 5 rows (widening triangle)
A
B B
C C
D D
E E | Item | Type | Description |
|---|---|---|
rows | int | Number of rows (row letter runs A through the rows-th letter). Clamp to 1–26 for A–Z demos. |
| Printed output | text | Widening alphabet triangle: each row mirrors the same letter with a growing gap - bottom row has 2*rows - 1 spaces between the two letters plus leading spaces. |
base = "A".charCodeAt(0)
for r from 0 to rows-1:
append (rows-1-r) leading spaces
ch = String.fromCharCode(base + r)
append ch
if r > 0:
append (2*r - 1) gap spaces
append ch
console.log(line) | Approach | Idea | Best for |
|---|---|---|
| Direct print with gap guard | Leading spaces, one letter, if r > 0 gap + mirror letter | Learning conditional row logic and gap formula |
| One-line row builder | pad + ch + ((gap + ch) if r > 0 else "") string expression | Clearer debugging and row inspection |
| Program 32 contrast | See Program 32 (palindrome rows) | Palindrome rows vs same-letter mirror pairs |
| Goal | Pattern |
|---|---|
| Leading spaces | line += " ".repeat(rows - 1 - r) |
| Outer loop (row index) | for (let r = 0; r < rows; r++) |
| Row letter | ch = String.fromCharCode(base + r) |
| Gap spaces (r > 0) | line += " ".repeat(2 * r - 1) |
| Mirror letter (r > 0) | line += ch inside if (r > 0) |
| End the row | console.log(line) |
| Row builder variant | pad + ch + ((" " * (2*r-1) + ch) if r > 0 else "") |
Three parts of every row - pick the mental model that clicks for you.
rows - 1 - r
centers rowTop row gets the most padding; bottom row aligns flush left before letters.
2*r - 1
1, 3, 5, 7...Widens the space between mirrored letters - row 1 gets 1, row 4 gets 7.
same ch
if r > 0Prints the same letter again after the gap - guarded by if r > 0 so row 0 stays a single A.
mirrored halves
different gapMirrored pattern with spaces between halves - see Program 19.
Reach for widening alphabet triangles when teaching conditional row logic, gap formulas, and symmetric same-letter pairs after palindrome pyramids.
Program 32 prints palindrome rows (A, ABA, ABCBA). This pattern mirrors the same letter with a widening gap instead.
Master the 2*r - 1 gap formula before tackling full diamonds and hollow shapes.
The (rows - 1 - r) space formula appears in centered stars, numbers, and diamond patterns.
Next pattern closes into a full alphabet diamond - another symmetric shape variation.
This is a console teaching pattern - not how you build modern app screens.
Key benefit: one program that combines centering spaces with a widening gap formula - the same two skills used in diamond patterns, hollow pyramids, and symmetric ASCII art far beyond alphabet demos.
Choose a row count between 1 and 26 and draw the widening alphabet triangle in the browser.
Three complete JavaScript programs - fixed five rows with leading spaces and a widening gap, prompt input, and a one-line row-builder variant for clarity. Click View Output to reveal sample console results, or Try it Yourself to run in the browser editor.
Print five rows of the widening alphabet triangle with leading spaces and a mirrored letter gap.
rows = 5Hard-coded height - ideal for first demos and screenshots.
let rows = 5;
rows = Math.max(1, Math.min(rows, 26));
const base = "A".charCodeAt(0);
for (let r = 0; r < rows; r++) {
let line = "";
line += " ".repeat(rows - 1 - r);
const ch = String.fromCharCode(base + r);
line += ch;
if (r > 0) {
line += " ".repeat(2 * r - 1);
line += ch;
}
console.log(line);
} The outer loop walks row index r from 0 to 4. For each row, line += " ".repeat(rows - 1 - r) centers the row, then ch = String.fromCharCode(base + r) picks the row letter. For r > 0, line += " ".repeat(2 * r - 1) widens the gap before the mirrored letter. Row 0 prints only A with four leading spaces; row 4 prints E E with a 7-space gap and no leading spaces.
Let the user choose the height at runtime.
Read rows and clamp to 1–26. Validate parseInt(prompt(), 10) with Number.isFinite in real apps.
let rows = parseInt(prompt("Enter number of rows (max 26):"), 10);
if (!Number.isFinite(rows)) {
console.log("Please enter a whole number.");
} else {
rows = Math.max(1, Math.min(rows, 26));
const base = "A".charCodeAt(0);
for (let r = 0; r < rows; r++) {
let line = "";
line += " ".repeat(rows - 1 - r);
const ch = String.fromCharCode(base + r);
line += ch;
if (r > 0) {
line += " ".repeat(2 * r - 1);
line += ch;
}
console.log(line);
}
} Same widening triangle core as Example 1; only the row count comes from prompt. Three rows produce A, B B, and C C with 2, 1, and 0 leading spaces respectively.
Build each row as a single string expression with pad, letter, and optional gap.
Combine pad, letter, and conditional gap in one row string - same logic, easier row inspection.
let rows = 5;
rows = Math.max(1, Math.min(rows, 26));
const base = "A".charCodeAt(0);
for (let r = 0; r < rows; r++) {
const pad = " ".repeat(rows - 1 - r);
const ch = String.fromCharCode(base + r);
const row = pad + ch + (r > 0 ? " ".repeat(2 * r - 1) + ch : "");
console.log(row);
} pad holds the leading spaces and ch is the row letter. The ternary adds the gap and second letter only when r > 0 - identical output to Examples 1 and 2, with the full row visible as one string for debugging.
Clamp rows, then set base = "A".charCodeAt(0) for the alphabet starting point.
line += " ".repeat(rows - 1 - r) centers row r before any letters.
Append ch = String.fromCharCode(base + r), then if r > 0 append " ".repeat(2 * r - 1) and ch again.
console.log(line) ends the row after leading spaces, letter, optional gap, and mirror finish; the outer loop advances r.
Total characters grow with gap spaces: row r prints O(r) gap spaces — O(n²) time, O(1) extra memory (loop version).
rows = 5Trace each outer-loop value of r and see how leading spaces, row letter, gap, and mirror letter produce each widening row.
r | letter | lead spaces | gap | mirror | full row |
|---|---|---|---|---|---|
| 0 | A | 4 | (none) | (none) | A |
| 1 | B | 3 | 1 | B | B B |
| 2 | C | 2 | 3 | C | C C |
| 3 | D | 1 | 5 | D | D D |
| 4 | E | 0 | 7 | E | E E |
Highlight rows: r = 0 (4 lead spaces, A only), r = 1 (3 spaces, B + 1 gap + B → B B), r = 4 (0 spaces, E + 7 gap + E). Gap grows by 2 each row: 2*r - 1 gives 1, 3, 5, 7 for rows 1–4.
Where widening alphabet triangles show up beyond the homework prompt.
Program 32 prints palindrome rows (A, ABA, ABCBA). This pattern mirrors the same letter with a widening gap instead.
Example: compare palindrome ABCBA rows vs B B / C C mirror pairs side by side.
Reinforce the 2*r - 1 gap formula before tackling hollow pyramids and full diamonds.
Example: trace row 2 (r=2) on paper: lead spaces=2, letter=C, gap=3, mirror=C.
Mirrored halves with spaces between - see Program 19.
Example: compare Program 19’s mirrored halves with this same-letter widening gap approach.
Mirror the triangle downward to close a full alphabet diamond.
Example: after the top half, loop r from rows-2 down to 0 with the same gap + mirror row logic.
Sum of widening gap spaces makes O(n²) concrete for beginners.
Example: 5 rows → gap spaces 0+1+3+5+7 = 16 plus 9 letters.
Classic nested-loop question that tests gap formula and centering spaces.
Example: explain why row 0 skips the gap and mirror without running code.
Pro Tip: say “leading spaces, letter, if r>0 gap then same letter” before coding - that story prevents double A on row 0 and wrong gap width.
Why this pattern earns a spot after the centered palindrome pyramid from Program 32.
if r > 0 gap guard - a pattern reused whenever row 0 is special.
Leading spaces create a visually balanced pyramid - every row aligns under the apex.
Direct print loops for learning; one-line row builder for clearer debugging.
Streaming output needs no storage beyond loop counters (row builder uses O(r) per row string).
Pro Tip: when row 0 prints only A, the mirror loop range is empty - that is correct, not a bug.
Small habits that keep widening alphabet triangle code clean.
Use ch = String.fromCharCode(base + r) - keeps gap and mirror loops readable.
parseInt(prompt(), 10) in try/exceptAvoid crashes when the user types letters instead of a number.
rows = Math.max(1, Math.min(rows, 26)) keeps demos inside A–Z.
if r > 0: must wrap gap and mirror - never print a second A on row 0.
Trace A, B B, C C with 2, 1, 0 leading spaces on paper before coding larger demos.
Pro Tip: if gaps look too narrow, check the formula - it should be 2*r - 1, not 2*r.
Mistakes that commonly break widening alphabet triangles.
Forgetting if r > 0 prints A A on row 0 - two letters at the apex.
→ Wrap gap and mirror in if r > 0: so row 0 prints only one A.
Using 2*r instead of 2*r - 1 makes gaps one space too wide starting at row 1.
→ Use 2*r - 1 for the gap - row 1 needs 1 space, row 4 needs 7.
Using r lead spaces or rows - r misaligns the triangle - rows lean or over-indent.
→ Use rows - 1 - r leading spaces so row 0 gets the most padding.
Non-numeric input yields NaN with bare parseInt(prompt(), 10).
→ Validate with Number.isFinite and clamp the range.
Printing gap + mirror on every row including r=0 produces A A instead of a single centered A.
→ Keep gap and mirror inside if r > 0: on every row.
Check these inputs before calling the solution done.
Output is just A with no leading spaces when rows=1 - gap block skipped because r=0.
Treat as invalid; re-prompt instead of silent empty output.
26 rows with letter Z - bottom row prints ...Z...Z... with no leading spaces.
Clamp to 26 or define a wrap/error policy before printing.
Use Number.isFinite before clamping rows.
Same loops work with base = "a".charCodeAt(0) and lowercase output.
Try these variations to lock in the pattern.
r prints 2r - 1 gap spaces when r > 0. Over n rows the total gap spaces sum to (n-1)².if r > 0: - without it row 0 would print A A instead of a single apex.Quick Takeaway: outer loop sets r, print (rows - 1 - r) spaces, print ch, if r > 0 print gap and ch again, then break the line - that is the whole widening alphabet triangle.
| Program | Time | Extra space |
|---|---|---|
| Direct print with gap guard (Examples 1–2) | O(rows²) | O(1) |
| Row builder variant (Example 3) | O(rows²) | O(r) for row string per row |
The widening alphabet triangle combines centering spaces with a growing gap and mirrored same-letter rows. Master the direct-print version, then try the one-line row builder for clearer debugging.
Practice the three examples above, then continue to Program 34 in the alphabet pattern series.
Print leading spaces, guard row 0 with if r > 0, use gap formula 2*r - 1, clamp rows to 26, and compare with Program 32 (centered palindrome pyramid).
base = "A".charCodeAt(0), clamp rows to 1–26(rows - 1 - r) leading spaces each rowch, then if r > 0: gap 2*r-1 and mirror chline += ch for letters; console.log(line) after each row"A".charCodeAt(0)2*r for gap - gaps one space too wideif r > 0 - row 0 prints two lettersPrint the widening triangle the beginner-friendly way.
letter + gap + letter
Definitionrows - 1 - r
Center2*r - 1
Codeif r > 0
CodeO(n²) time
AnalysisRow r prints (rows - 1 - r) leading spaces, then letter String.fromCharCode('A'.charCodeAt(0) + r). For r > 0, a gap of 2*r - 1 spaces separates a second copy of the same letter. Row 0 is a single centered A.
Next up: the alphabet diamond - extend this widening triangle into a full symmetric shape.
12 people found this page helpful