Palindrome Rule
A..peak..A
Each row ascends from A to the peak, then descends from peak - 1 back to A.

Each row is a centered palindrome of letters: row 0 prints A, row 1 prints ABA, row 2 prints ABCBA, until the bottom row shows ABCDEDCBA for five rows. Leading spaces (rows - 1 - r) center the pyramid; ascend and descend loops mirror letters without duplicating the peak. Compare with Program 18 (left-aligned palindrome pyramid). Includes a live preview, worked JavaScript examples, edge cases, and complexity.
A..peak..A
Each row ascends from A to the peak, then descends from peak - 1 back to A.
rows - 1 - r
line += " ".repeat(rows - 1 - r) centers each palindrome row under the apex.
A..peak
for (let code = base; code <= peak; code++) appends letters up to and including the peak.
peak-1..A
for (let code = peak - 1; code >= base; code--) mirrors back without repeating the peak.
1–26 rows
Pick a row count and draw the centered palindrome pyramid in the browser instantly.
Complexity
Row r prints 2r + 1 letters; total letters = n² ≈ O(n²); extra memory stays O(1).
A centered alphabet palindrome pyramid prints each row as a mirror string of letters, padded with leading spaces so the shape is centered. Row 0 prints A; each next row adds one more letter to the peak and mirrors back - ABA, ABCBA, and so on.
In JavaScript you solve it with an outer loop over row index r, a space prefix, two inner loops for ascend and descend, and charCodeAt(0)/String.fromCharCode() - or build left and right strings and concatenate for clarity.
It combines three classic pattern skills - centering with spaces, ascending sequences, and mirror loops that skip the peak - the same building blocks used in diamonds, hollow pyramids, and symmetric ASCII art. Compare with Program 18 to see how centering transforms the same palindrome rows.
" " * (rows - 1 - r) - row 0 gets rows - 1 spaces; bottom row gets none.
for (code = base; code <= peak; code++) - appends A through the row peak letter.
for (code = peak - 1; code >= base; code--) - mirrors back starting below the peak.
Descend starts at peak - 1 so ABCBA stays a true palindrome.
In short: set base = "A".charCodeAt(0), loop r from 0 to rows - 1, append (rows - 1 - r) spaces, ascend A..peak, descend (peak-1)..A, then console.log(line) for the newline.
Given a positive integer rows, print a centered pyramid of rows lines. Row r prints (rows - 1 - r) leading spaces, then letters from A up to String.fromCharCode("A".charCodeAt(0) + r), then back down from peak - 1 to A.
// First 5 rows (centered)
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA | Item | Type | Description |
|---|---|---|
rows | int | Number of rows (peak letter runs A through the rows-th letter). Clamp to 1–26 for A–Z demos. |
| Printed output | text | Centered palindrome pyramid: each row is a mirror string with leading spaces - widest row has 2*rows - 1 letters. |
base = "A".charCodeAt(0)
for r from 0 to rows-1:
append (rows-1-r) spaces
peak = base + r
for code from base to peak: append String.fromCharCode(code)
for code from peak-1 down to base: append String.fromCharCode(code)
console.log(line) | Approach | Idea | Best for |
|---|---|---|
| Ascend + descend loops | Two for loops with line += String.fromCharCode(code) | Learning mirror loops and peak-off-by-one |
| Left/right strings | Build left/right strings then concatenate | Clearer debugging and row inspection |
| Program 18 contrast | See Program 18 (left-aligned palindrome) | Same palindrome logic without centering spaces |
| Goal | Pattern |
|---|---|
| Leading spaces | line += " ".repeat(rows - 1 - r) |
| Outer loop (row index) | for (let r = 0; r < rows; r++) |
| Peak letter code | peak = base + r |
| Ascend loop | for (let code = base; code <= peak; code++) line += String.fromCharCode(code) |
| Descend loop | for (let code = peak - 1; code >= base; code--) line += String.fromCharCode(code) |
| End the row | console.log(line) |
| String variant | " ".repeat(...) + left + right |
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.
base..peak
A, AB, ABC...Prints letters from A up to and including the row peak.
peak-1..base
mirror halfWalks back down from one below the peak - avoids duplicating the center letter.
Reach for centered palindrome pyramids when teaching mirror loops, spacing, and symmetric row building after diagonal patterns.
Program 31 places letters on two diagonals forming an X. This pattern builds full palindrome strings centered with leading spaces.
Master the peak - 1 start index before tackling full diamonds and hollow shapes.
The (rows - 1 - r) space formula appears in centered stars, numbers, and diamond patterns.
Next pattern widens letter pairs - another symmetric triangle variation.
This is a console teaching pattern - not how you build modern app screens.
Key benefit: one program that combines centering spaces with mirror loops - 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 centered alphabet palindrome pyramid in the browser.
Three complete JavaScript programs - fixed five rows with centering spaces and mirror loops, prompt input, and a left/right string-build 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 centered alphabet palindrome pyramid with leading spaces and mirror loops.
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++) { // 0..4
let line = "";
// Centering spaces
line += " ".repeat(rows - 1 - r);
const peak = base + r;
// Ascend: A..peak
for (let code = base; code <= peak; code++) {
line += String.fromCharCode(code);
}
// Descend: (peak-1)..A
for (let code = peak - 1; code >= base; code--) {
line += String.fromCharCode(code);
}
console.log(line);
} The outer loop walks row index r from 0 to 4. For each row, line += " ".repeat(rows - 1 - r) centers the palindrome, then peak = base + r sets the row peak letter. The ascend loop appends A through the peak; the descend loop mirrors from peak - 1 back to A without duplicating the center. Row 0 prints only A with four leading spaces; row 4 prints the full ABCDEDCBA with no 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 peak = base + r;
for (let code = base; code <= peak; code++) {
line += String.fromCharCode(code);
}
for (let code = peak - 1; code >= base; code--) {
line += String.fromCharCode(code);
}
console.log(line);
}
} Same centered palindrome core as Example 1; only the row count comes from prompt. Three rows produce A, ABA, and ABCBA with 2, 1, and 0 leading spaces respectively.
Build left and right halves as strings, then log spaces + left + right.
Build ascend and descend halves as strings - 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 peak = base + r;
let left = "";
for (let code = base; code <= peak; code++) {
left += String.fromCharCode(code);
}
let right = "";
for (let code = peak - 1; code >= base; code--) {
right += String.fromCharCode(code);
}
console.log(" ".repeat(rows - 1 - r) + left + right);
} left builds the ascend half and right builds the descend half as strings. Concatenating with leading spaces produces identical output to Examples 1 and 2, but you can inspect left and right separately during 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.
Ascend A..peak, then descend (peak-1)..A with line += String.fromCharCode(code) in both loops.
console.log(line) ends the row after spaces and both letter loops finish; the outer loop advances r.
Total letters: 1 + 3 + 5 + ... + (2n-1) = n² — O(n²) time, O(1) extra memory (loop version).
rows = 5Trace each outer-loop value of r and see how leading spaces, peak letter, ascend, and descend produce each centered palindrome row.
r | peak | spaces | ascend | descend | full row |
|---|---|---|---|---|---|
| 0 | A | 4 | A | (none) | A |
| 1 | B | 3 | AB | A | ABA |
| 2 | C | 2 | ABC | BA | ABCBA |
| 3 | D | 1 | ABCD | CBA | ABCDCBA |
| 4 | E | 0 | ABCDE | DCBA | ABCDEDCBA |
Highlight rows: r = 0 (4 spaces, peak A, A only), r = 2 (2 spaces, peak C, ABCBA), r = 4 (0 spaces, peak E, ABCDEDCBA). Total letters printed: 1 + 3 + 5 + 7 + 9 = 25 = rows² for rows = 5.
Where centered palindrome pyramids show up beyond the homework prompt.
Program 31 uses diagonal columns for an X shape. This pattern builds full palindrome strings centered with spaces.
Example: compare diagonal X grid vs centered ABCBA rows side by side.
Reinforce the peak - 1 descend start before tackling diamonds and hollow pyramids.
Example: trace row 2 (r=2) on paper: spaces=2, ascend=ABC, descend=BA.
Same palindrome rows without centering - see Program 18.
Example: add line += " ".repeat(rows - 1 - r) to Program 18 to get this shape.
Mirror the pyramid downward to close a full alphabet diamond.
Example: after the top half, loop r from rows-2 down to 0 with the same row logic.
Sum of odd row lengths makes O(n²) concrete for beginners.
Example: 5 rows → 25 letters printed (1+3+5+7+9).
Classic nested-loop question that tests mirror logic and centering spaces.
Example: explain why descend starts at peak - 1 without running code.
Pro Tip: say “spaces, then up to peak, then down from peak minus one” before coding - that story prevents duplicated peaks and wrong indentation.
Why this pattern earns a spot after the alphabet X pattern from Program 31.
Ascend then descend with peak - 1 - a pattern reused in diamonds and hollow shapes.
Leading spaces create a visually balanced pyramid - every row aligns under the apex.
Direct print loops for learning; left/right string variant for clearer debugging.
Streaming output needs no storage beyond loop counters (string variant uses O(r) per row).
Pro Tip: when row 0 prints only A, the descend loop range is empty - that is correct, not a bug.
Small habits that keep centered palindrome pyramid code clean.
Use peak = base + r - keeps ascend and descend 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.
line += " ".repeat(rows - 1 - r) must run before the ascend loop each row.
Trace A, ABA, ABCBA with 2, 1, 0 spaces on paper before coding larger demos.
Pro Tip: if the pyramid leans left, check the space count - it should be rows - 1 - r, not r.
Mistakes that commonly break centered alphabet palindrome pyramids.
Starting descend at peak instead of peak - 1 prints ABCCBA - a doubled center letter.
→ Use for (let code = peak - 1; code >= base; code--) so the peak appears only once.
Using r spaces or rows - r misaligns the pyramid - rows lean or over-indent.
→ Use rows - 1 - r leading spaces so row 0 gets the most padding.
Magic numbers like String.fromCharCode(65 + r) work but break readability and lowercase variants.
→ Use base = "A".charCodeAt(0) and String.fromCharCode(base + r) instead of raw ASCII values.
Non-numeric input yields NaN with bare parseInt(prompt(), 10).
→ Validate with Number.isFinite and clamp the range.
Printing only palindrome letters without spaces produces a left-aligned pyramid like Program 18.
→ Print " " * (rows - 1 - r) before the ascend loop on every row.
Check these inputs before calling the solution done.
Output is just A (with rows - 1 spaces) - descend loop range is empty when peak equals base.
Treat as invalid; re-prompt instead of silent empty output.
26 rows with peak 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.
left and right stringsr prints 2r + 1 letters. Over n rows the total is n².for (let code = peak - 1; code >= base; code--) - include A by stopping when code < base.left + right string variant is equivalent to the direct-print version - use whichever fits your lesson.Quick Takeaway: outer loop sets r, print (rows - 1 - r) spaces, ascend A..peak, descend (peak-1)..A, then break the line - that is the whole centered palindrome pyramid.
| Program | Time | Extra space |
|---|---|---|
| Ascend + descend loops (Examples 1–2) | O(rows²) | O(1) |
| String variant (Example 3) | O(rows²) | O(r) for left/right strings per row |
The centered alphabet palindrome pyramid combines centering spaces with mirror loops - ascend A..peak, descend (peak-1)..A. Master the direct-print version, then try the left/right string variant for clearer debugging.
Practice the three examples above, then continue to Program 33 in the alphabet pattern series.
Print leading spaces, run ascend and descend loops with peak - 1, clamp rows to 26, and compare with Program 18 (left-aligned palindrome pyramid).
base = "A".charCodeAt(0), clamp rows to 1–26(rows - 1 - r) leading spaces each rowfor (code = base; code <= peak; code++), descend for (code = peak - 1; code >= base; code--)line += String.fromCharCode(code) in letter loops; console.log(line) afterpeak - duplicates the center letter"A".charCodeAt(0)console.log inside the letter loopsPrint the centered pyramid the beginner-friendly way.
A..peak..A
Definitionrows - 1 - r
Centerbase..peak
Codepeak-1..base
CodeO(n²) time
AnalysisRow r prints (rows - 1 - r) leading spaces, then letters from A up to the peak String.fromCharCode('A'.charCodeAt(0) + r), then back down to A starting at peak - 1 so the peak is not duplicated.
Next up: the widening alphabet triangle - build on symmetric shape ideas from this tutorial.
12 people found this page helpful