Reverse Centered Alphabet Pyramid in JavaScript

Beginner
⏱️ 12 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Two phases

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

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 E
1

Node.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.

JavaScript
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);
2

Browser version (document.write + div)

Reference-style output using fixed-width cells.

HTML
<!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

1

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.

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.

Key Takeaways

1

Two phases: down (E..A) then up (B..E) to close the pyramid.

2

Two sweeps per row keep width fixed and the grid symmetric.

3

j > i chooses outer letter vs row letter.

4

Time complexity: O(n²).

❓ Frequently Asked Questions

Program 28 prints only the bands from 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.
The middle row happens when i = A. With the condition j > i, most positions keep their outer column letter, while the center position becomes A.
HTML collapses normal whitespace, so gaps don’t align reliably. Fixed-width div cells preserve the grid and keep columns aligned.

More JavaScript alphabet patterns

Browse the full list for more variations.

Next: Program 30 →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful