Mirror Halves With Spacing

What You'll Learn
This program prints two mirrored halves in each row. The left half grows from A to the row letter. The right half shrinks from the row letter back to A. Between them, we print blank cells so the halves stay centered and aligned.
⭐ Pattern Output
A A
AB BA
ABC CBA
ABCD DCBA
ABCDEEDCBANode.js / console version
In Node, it’s easiest to build strings using spaces for the “blank cells”.
const A = "A".charCodeAt(0);
const top = "E".charCodeAt(0);
for (let i = A; i <= top; i++) {
let left = "";
for (let j = A; j <= top; j++) left += j <= i ? String.fromCharCode(j) : " ";
let right = "";
for (let k = top; k >= A; k--) right += k > i ? " " : String.fromCharCode(k);
console.log(left + right);
}Browser version (document.write + div)
This matches the reference: fixed-width cells (empty <div>) create the spaces between halves.
<!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 = A; j <= top; j++) {
if (j <= i) document.write(\"<div class=\\\"cell\\\">\" + String.fromCharCode(j) + \"</div>\");
else document.write(\"<div class=\\\"cell\\\"></div>\");
}
for (let k = top; k >= A; k--) {
if (k > i) document.write(\"<div class=\\\"cell\\\"></div>\");
else document.write(\"<div class=\\\"cell\\\">\" + String.fromCharCode(k) + \"</div>\");
}
document.write(\"<br>\");
}
</script>
</body>
</html>🧠 How It Works
Outer loop: i from A to E
Each row grows the visible letters by one step.
Left half: j from A to E
If j <= i, print the letter; otherwise print a space (blank cell).
Right half: k from E to A
If k > i print space; else print the mirrored letter (k descending).
Shrinking center gap
The blank gap shrinks by two each row until the last line becomes ABCDEEDCBA.
Key Takeaways
Two halves per row: ascending and descending.
Blank cells maintain alignment.
Last row has no blanks: ABCDEEDCBA.
Time complexity is O(n²).
❓ Frequently Asked Questions
More JavaScript alphabet patterns
Browse the full list or try star and number pattern tutorials.
The gap shrinks by 2 cells each row because you add one cell to the left half and one to the right half.
10 people found this page helpful
