Centered Palindrome Pyramid

⭐ Pattern Output
Output
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA1
Node.js / console version
JavaScript
const A = "A".charCodeAt(0);
const top = "E".charCodeAt(0);
for (let i = A; i <= top; i++) {
let line = "";
// indentation (two spaces per missing cell)
for (let j = top; j >= i; j--) line += " ";
for (let k = A; k <= i; k++) line += String.fromCharCode(k);
for (let k = i - 1; k >= A; k--) line += String.fromCharCode(k);
console.log(line);
}2Browser version (
Browser version (document.write + div)
Matches the reference output using blank cells and fixed-width letter cells.
HTML
<!DOCTYPE html>
<html>
<head>
<style>
div.cell {
display: inline-block;
text-align: center;
width: 13px;
}
</style>
</head>
<body>
<script>
const A = 65;
const top = 69;
for (let i = A; i <= top; i++) {
for (let j = top; j >= i; j--) document.write("<div class=\\"cell\\"></div>");
for (let k = A; k <= i; k++) document.write("<div class=\\"cell\\">" + String.fromCharCode(k) + "</div>");
for (let k = i - 1; k >= A; k--) document.write("<div class=\\"cell\\">" + String.fromCharCode(k) + "</div>");
document.write("<br>");
}
</script>
</body>
</html>🧠 How It Works
1
Core idea
Outer i runs from A to E. First loop prints leading blank cells for centering. Second loop prints A..i. Third loop prints (i-1)..A (using a decrementing counter) so the peak letter isn’t duplicated, producing palindromes like ABCDCBA.
Logic
2
Loops + condition
An outer loop picks the row, inner loop(s) decide what to print (letter / star / blank) based on comparisons.
Nested loops
=
Connect it to the output
Compare each loop boundary with the pattern output above — each row corresponds to one outer iteration.
❓ Frequently Asked Questions
The first half already prints the peak letter
i. Starting the mirror at i - 1 prevents duplicates (for example ABCDCBA instead of ABCCDCBA).The leading blanks center each row. As
i grows, we print fewer blanks, so the pyramid stays aligned.Yes. Change
top from E to a later letter (like "G".charCodeAt(0)), and keep the same three-step row logic: indent, A..i, then i-1..A.More JavaScript alphabet patterns
Browse the full list for more variations.
10 people found this page helpful
