Shape Rule
Palindrome rows
A, BAB, CBABC, … EDCBABCDE.

Build each row as a palindrome by stitching a descending run (i down to B) with an ascending run (A up to i), so a single A sits at the center. This version prints letters with no extra spacing (unlike right-aligned patterns that use 2-column cells). Compare Program 18 (another palindrome style) and Program 16 (centering with pads). Includes a live preview, worked JavaScript examples, edge cases, and complexity.
Palindrome rows
A, BAB, CBABC, … EDCBABCDE.
Down, then up
Descend i..B; ascend A..i.
j > 'A'
Descending stops before A so the center is not doubled.
1, 3, 5…
Row length is 2r-1 for row number r.
1–8 rows
Pick a height and draw the palindrome pyramid.
Complexity
Total printed characters = n² → O(n²).
A palindromic alphabet pyramid prints rows that read the same forwards and backwards, with peak letters growing from A outward around a single center A.
In JavaScript you solve it with nested loops over letter codes: descend from the row peak to B, then ascend from A to the peak using charCodeAt/fromCharCode.
It teaches how two ranges join without duplicating the center — a common bug when building palindrome strings or patterns.
Left wing: peak down to B.
Center A plus right wing.
Stop descending before A.
No pads in the classic sample.
In short: for each peak i, append i..B, then A..i, then call console.log(line).
Given a row count n (or fixed A–E), print a left-aligned palindromic alphabet pyramid with peak letters growing each row.
// Five rows (left-aligned; no cell padding)
// A
// BAB
// CBABC
// DCBABCD
// EDCBABCDE | Item | Type | Description |
|---|---|---|
n | int | Number of rows (1..26 for A..Z). Peak of row r is String.fromCharCode("A".charCodeAt(0) + r). |
| Printed output | text | Palindrome rows of odd length 1, 3, 5, … |
for i from base to peak:
line = ""
for j from i down while j > base:
line += String.fromCharCode(j)
for j from base to i:
line += String.fromCharCode(j)
console.log(line) | Approach | Idea | Best for |
|---|---|---|
| Descend then ascend (this page) | i..B then A..i | Matching the BAB / EDCBABCDE sample |
| Ascend then descend | A..peak then peak-1..A | Program 18-style palindromes |
| Goal | Pattern |
|---|---|
| Outer rows | for (let i = base; i <= top; i++) |
| Left wing | for (let j = i; j > base; j--) line += String.fromCharCode(j) |
| Center + right | for (let j = base; j <= i; j++) line += String.fromCharCode(j) |
| End row | console.log(line) |
| Other palindrome | See Program 18 |
Same row - three roles that build the palindrome.
leftDescending wing; stops before A
rightCenter A plus ascending wing
onceGuarantees a single center A
breakEnds the row after both wings
Reach for this when teaching palindrome construction with two joined ranges.
Reuse descending ranges, then mirror them ascending.
Practice stopping one loop so the join character prints once.
Same palindrome idea; different wing order.
Show why 1+3+5+…+(2n-1) equals n².
This is a console teaching pattern — not how you build modern app screens.
Key benefit: stopping the descending loop before A is the cleanest way to keep a single center character in this style of palindrome.
Choose between 1 and 8 rows and draw the palindromic alphabet pyramid in the browser.
Three complete JavaScript programs - fixed A–E, user-chosen row count, and a spaced variant. Click View Output to reveal sample console results, or Try it Yourself to run in the browser editor.
Print five palindromic rows from A through E.
Two loops per row: descending (i..B), then ascending (A..i). The descending loop stops before A, so A appears once.
const base = "A".charCodeAt(0);
const top = "E".charCodeAt(0);
for (let i = base; i <= top; i++) {
let line = "";
for (let j = i; j > base; j--) {
line += String.fromCharCode(j);
}
for (let j = base; j <= i; j++) {
line += String.fromCharCode(j);
}
console.log(line);
} When i is 'C', the left wing appends C B and the right wing appends A B C → CBABC. The center A comes only from the ascending loop.
Let the user choose how many rows to print.
Cap at 26 for A–Z. Validate parseInt(prompt()) with Number.isFinite in real apps.
let n = parseInt(prompt("Enter number of rows (1..26):"), 10);
if (!Number.isFinite(n)) {
console.log("Please enter a whole number.");
} else {
n = Math.max(1, Math.min(n, 26));
const base = "A".charCodeAt(0);
for (let r = 0; r < n; r++) {
const peak = base + r;
let line = "";
for (let ch = peak; ch > base; ch--) {
line += String.fromCharCode(ch);
}
for (let ch = base; ch <= peak; ch++) {
line += String.fromCharCode(ch);
}
console.log(line);
}
} Row index r maps to peak "A".charCodeAt(0) + r. Same descend/ascend rules; only the number of peaks changes.
Same palindrome with a space after each letter for a wider look.
Useful when you want the shape to read more clearly in dense terminals.
const base = "A".charCodeAt(0);
const top = "E".charCodeAt(0);
for (let i = base; i <= top; i++) {
let line = "";
for (let j = i; j > base; j--) {
line += String.fromCharCode(j) + " ";
}
for (let j = base; j <= i; j++) {
line += String.fromCharCode(j) + " ";
}
console.log(line);
} Same wing logic; only the append format adds a trailing space after each letter. Trim the final space per row if you need a clean line end.
i goes from A to E (or your chosen height).
Loop j = i..B appends the descending part. We stop at j > base so A is not appended here.
Loop j = A..i appends the center A and then climbs back up to the peak letter.
console.log(line) ends the row so the next peak can grow both wings.
Row lengths are 1, 3, 5, 7, 9 for A..E, so total characters is n² for n rows.
Trace each row’s left wing, center+right wing, and full line.
i | Left (i..B) | Right (A..i) | Printed row |
|---|---|---|---|
A | (empty) | A | A |
B | B | A B | BAB |
C | C B | A B C | CBABC |
D | D C B | A B C D | DCBABCD |
E | E D C B | A B C D E | EDCBABCDE |
Lengths: 1+3+5+7+9 = 25 = 5².
Where this palindromic pyramid shows up beyond the homework prompt.
Clearest demo of joining two ranges around one center.
Example: flip > to >= and watch AA appear.
Same mirror idea; different wing construction order.
Example: print both for n = 5 and compare.
Outer and inner loops over ascending and descending char ranges.
Example: rewrite with int indexes for peak and offset.
Add spaces or leading pads without changing the letter order.
Example: Example 3 spaced letters; Program 16 for centering.
Odd-length rows make the n² total easy to see.
Example: 5 rows print 25 characters.
Next pattern switches back to a running sequential stream.
Example: continue to Program 25.
Pro Tip: say “down to B, then A up to the peak” before coding - that story prevents a duplicated center A.
Why this pattern earns a spot after reverse and sequential pyramids.
A duplicated center or wrong wing order shows up immediately.
Descend and ascend are easy to explain separately.
A natural bridge to string-palindrome thinking.
Streaming output needs no storage beyond loop variables.
Pro Tip: learn the compact no-space version first; treat spacing and centering as optional visual upgrades afterward.
Small habits that keep palindromic alphabet pyramids clean.
That single condition is what prevents a double center.
Beyond Z you need a wrap/stop policy.
Number.isFiniteAvoid crashes when the user types letters instead of a number.
Classic output is left-aligned; add pads if you want a centered look.
If rows look like ABCBA instead of CBABC, you used the other wing order.
Pro Tip: if you see BAAB or CBABBC, the descending loop almost certainly used >= 'A'.
Mistakes that commonly break palindromic alphabet pyramids.
Duplicates the center A (BAAB, CBABBC, …).
→ Keep the descending condition as j > base.
You may get Program 18-style rows instead of BAB / CBABC.
→ Descend first, then ascend for this sample.
Classic rows hug the left margin.
→ Add leading spaces (or see Program 16) for a centered look.
Letters or empty input yield NaN.
→ Validate parseInt(prompt()) with Number.isFinite and re-prompt on failure.
Peaks walk past Z without a defined policy.
→ Cap at 26 or define wrap/stop behavior.
Check these inputs before calling the solution done.
Output is just A (left wing empty).
Through EDCBABCDE (25 characters).
Through DCBABCD (Example 2).
Peak reaches Z; still one center A per row.
parseInt(prompt()) yields NaN - check with Number.isFinite.
Same loops with base 'a'.
Try these variations to lock in the pattern.
j > base on the descending loop - never >= for this sample.Quick Takeaway: for each peak, print descending letters down to B, then ascending letters from A to the peak, then break the line.
| Program | Time | Extra space |
|---|---|---|
| Fixed / input (Examples 1–2) | O(n²) | O(1) |
| Spaced letters (Example 3) | O(n²) | O(1) |
Total printed characters are 1+3+…+(2n-1) = n², so time is O(n²).
The palindromic alphabet pyramid is a small nested-loop exercise with lasting payoff: descending and ascending wings joined around a single center A. Master the classic A…E sample, then try user input and spaced letters.
Practice the three examples above, then continue to Program 25’s sequential decreasing alphabet triangle.
Descend to B, ascend from A, keep one center A with j > base, then break the line.
j > baseA..i for the center and right wingparseInt(prompt()) with Number.isFinite for user inputj >= 'A' on the left wingconsole.log(line) inside either wing loopPrint the palindromic alphabet pyramid the beginner-friendly way.
Down then up
DefinitionOne A only
Codej > 'A'
CodeEnds each row
I/On² chars
AnalysisOuter i runs A.. E. First inner loop appends the left wing (i down to B) by stopping at j > base. Second inner loop appends A.. i. Because the reverse loop stops before A, the center A is not duplicated.
Next up: sequential decreasing alphabet triangles with a running k++ stream.
12 people found this page helpful