Hollow Pyramid (A..E)

Beginner
⏱️ 10 min read
📚 Updated: Aug 2025
🎯 2 Code Examples

⭐ Pattern Output

Output
    A
   B B
  C   C
 D     D
E       E
1

Node.js / console version

JavaScript
const A = "A".charCodeAt(0);
const top = "E".charCodeAt(0);

for (let i = A; i <= top; i++) {
  let line = "";

  for (let j = top; j >= A; j--) line += (i === j ? String.fromCharCode(j) : " ");
  for (let k = A + 1; k <= top; k++) line += (i === k ? String.fromCharCode(k) : " ");

  console.log(line);
}
2

Browser version (document.write)

Reference-style output uses &nbsp; for spacing.

HTML
<!DOCTYPE html>
<html>
<body>
<script>
  const A = 65;
  const last = 69;
  for (let i = A; i <= last; i++) {
    for (let j = last; j >= A; j--) {
      if (i === j) document.write(String.fromCharCode(j));
      else document.write("&nbsp;&nbsp;");
    }
    for (let k = A + 1; k <= last; k++) {
      if (i === k) document.write(String.fromCharCode(k));
      else document.write("&nbsp;&nbsp;");
    }
    document.write("<br>");
  }
</script>
</body>
</html>

🧠 How It Works

1

Core idea

Outer i runs from A to E. First inner loop scans j = E..A and prints the letter when i === j, otherwise prints blanks. Second inner loop scans k = B..E and prints the letter when i === k. Together these form two diagonals that spread outward each row.

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

They mark the left and right boundary of the hollow pyramid; the spaces between make it hollow.
At the top, both diagonals overlap at the same position, so only A is printed.
HTML collapses spaces, so use &nbsp; or fixed-width cells to preserve alignment.

More JavaScript alphabet patterns

Browse the full list for more variations.

Next: Program 34 →

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