Reverse Centered Alphabet Pyramid in JavaScript

What You'll Learn
This reverse centered alphabet pyramid extends program 28: print rows for E..A, then the same row routine for B..E so the center row is not duplicated. Each row uses two sweeps and the condition j > i.
⭐ Pattern Output
E E E E E E E E E
E D D D D D D D E
E D C C C C C D E
E D C B B B C D E
E D C B A B C D E
E D C B B B C D E
E D C C C C C D E
E D D D D D D D E
E E E E E E E E ENode.js / console version
Print the upper phase (E..A) then the lower phase (B..E) to complete the reverse centered pyramid. Each row uses left + right sweeps with the same rule.
const A = "A".charCodeAt(0);
const top = "E".charCodeAt(0);
function printRow(i) {
let line = "";
for (let j = top; j >= A; j--) line += (j > i ? String.fromCharCode(j) : String.fromCharCode(i)) + " ";
for (let j = A + 1; j <= top; j++) line += (j > i ? String.fromCharCode(j) : String.fromCharCode(i)) + " ";
console.log(line.trimEnd());
}
for (let i = top; i >= A; i--) printRow(i);
for (let i = A + 1; i <= top; i++) printRow(i);Browser version (document.write + div)
Reference-style output using fixed-width cells.
<!DOCTYPE html>
<html>
<head>
<style>
div.cell {
display: inline-block;
text-align: center;
width: 15px;
}
</style>
</head>
<body>
<script>
const A = 65;
const top = 69;
function printRow(i) {
for (let j = top; j >= A; j--) {
if (j > i) document.write("<div class=\\"cell\\">" + String.fromCharCode(j) + "</div>");
else document.write("<div class=\\"cell\\">" + String.fromCharCode(i) + "</div>");
}
for (let j = A + 1; j <= top; j++) {
if (j > i) document.write("<div class=\\"cell\\">" + String.fromCharCode(j) + "</div>");
else document.write("<div class=\\"cell\\">" + String.fromCharCode(i) + "</div>");
}
document.write("<br>");
}
for (let i = top; i >= A; i--) printRow(i);
for (let i = A + 1; i <= top; i++) printRow(i);
</script>
</body>
</html>🧠 How It Works
Core idea
Reverse centered pyramid from program 28 row logic plus a second phase: rows for i = E..A, then rows for i = B..E so the center line is not repeated. Each row uses two inner sweeps (left and right of center) with j > i to pick the column letter vs the floor letter.
Loops + condition
An outer loop picks the row, inner loop(s) decide what to print (letter / star / blank) based on comparisons.
Connect it to the output
Compare each loop boundary with the pattern output above — each row corresponds to one outer iteration.
Key Takeaways
Two phases: down (E..A) then up (B..E) to close the pyramid.
Two sweeps per row keep width fixed and the grid symmetric.
j > i chooses outer letter vs row letter.
Time complexity: O(n²).
❓ Frequently Asked Questions
E down to A. Program 29 adds the return phase (B..E) with the same row builder, producing a reverse centered alphabet pyramid without repeating the center line.i = A. With the condition j > i, most positions keep their outer column letter, while the center position becomes A.div cells preserve the grid and keep columns aligned.More JavaScript alphabet patterns
Browse the full list for more variations.
10 people found this page helpful
