Palindrome Alphabet Pyramid in JavaScript

What You'll Learn
This pattern prints a centered palindrome per row: first it grows from A up to the row letter (A..i), then it mirrors back down (i-1..A). Starting the second loop at i - 1 avoids duplicating the middle character.
⭐ Pattern Output
A
ABA
ABCBA
ABCDCBA
ABCDEDCBANode.js / console version
Two inner loops: forward A..i, then reverse i-1..A:
const A = "A".charCodeAt(0);
const top = "E".charCodeAt(0);
for (let i = A; i <= top; i++) {
let line = "";
for (let j = A; j <= i; j++) line += String.fromCharCode(j);
for (let k = i - 1; k >= A; k--) line += String.fromCharCode(k);
console.log(line);
}Browser version (document.write)
Same logic as the reference, using character codes:
<!DOCTYPE html>
<html>
<body>
<script>
const A = 65;
const top = 69;
for (let i = A; i <= top; i++) {
for (let j = A; j <= i; j++) document.write(String.fromCharCode(j));
for (let k = i - 1; k >= A; k--) document.write(String.fromCharCode(k));
document.write("<br>");
}
</script>
</body>
</html>🧠 How It Works
Outer loop: rows
i goes from A to E.
Forward: A..i
Print letters from A up to the row letter.
Mirror: i-1..A
Start at i - 1 to avoid duplicating the peak letter.
Palindrome row
Every row reads the same forwards and backwards.
💡 Tips for Enhancement
Try These
- Change
topto print more rows (e.g.'G') - Add spaces between letters to make the symmetry easier to see
- Compare with Program 15 (another mirror-style pattern)
Avoid
- Starting the second loop at
i(it duplicates the peak letter) - Mixing lowercase/uppercase codes without updating bounds
Key Takeaways
Two loops per row: forward then reverse.
k = i - 1 prevents duplicate centers.
Row lengths are odd: 1, 3, 5, 7, 9.
Total characters printed: 25 for A..E.
❓ Frequently Asked Questions
i. Starting at i - 1 produces ABCBA instead of ABCCBA.n rows the work is \(1 + 3 + \u2026 + (2n-1) = n^2\), so O(n²).'a'.charCodeAt(0) as the start and adjust the end to 'e'.More JavaScript alphabet patterns
Browse the full list or try star and number pattern tutorials.
The total characters printed for A..E is \(5^2 = 25\) because the row lengths are the first five odd numbers.
10 people found this page helpful
