Consecutive Letters Pyramid (A to O)

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

What You'll Learn

This pattern prints a right-aligned pyramid where letters continue across rows (A to O). The trick is a global counter k that starts at A and increments each time a letter is printed.

⭐ Pattern Output

Output
        A
      B C
    D E F
  G H I J
K L M N O
1

Node.js / console version

In Node, we can use spaces for blanks and build each line as a string.

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

for (let i = A; i <= top; i++) {
  let line = "";
  for (let j = top; j >= A; j--) {
    if (j > i) line += "  ";
    else line += String.fromCharCode(k++) + " ";
  }
  console.log(line.trimEnd());
}
2

Browser version (document.write + div)

Matches the reference: use fixed-width cells for alignment and a global k that increments.

HTML
<!DOCTYPE html>
<html>
<head>
  <style>
    div.cell {
      display: inline-block;
      text-align: center;
      width: 18px;
    }
  </style>
</head>
<body>
<script>
  const A = 65;
  const top = 69;
  let k = A;
  for (let i = A; i <= top; i++) {
    for (let j = top; j >= A; j--) {
      if (j > 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

1

Outer loop: i from A to E

Row i contains 1, 2, 3, 4, then 5 letters (right-aligned).

5 rows
2

Scan columns: j from E to A

The loop walks five fixed positions; early positions become blanks until j <= i.

Columns
3

Global counter: k++

When j <= i, print String.fromCharCode(k++) so letters continue across rows (A..O).

Continuous
=

Right-aligned stream

Blanks shift the letters to the right, while k keeps the alphabet flowing.

Key Takeaways

1

Use a global k to keep letters continuous across rows.

2

Print blanks while j > i to right-align.

3

There are \(1+2+3+4+5=15\) letters total (A through O).

4

Time complexity: O(n²).

❓ Frequently Asked Questions

With 5 rows, the number of letters printed is 15, so the last printed letter is the 15th letter starting from A: O.
Change top to "F".charCodeAt(0) (or 70). You’ll print \(1+2+3+4+5+6=21\) letters (A through U).
HTML collapses multiple spaces. Use fixed-width cells (or &nbsp;) to keep alignment.

More JavaScript alphabet patterns

Keep practicing nested loops with the next programs.

Next: Program 23 →
Did you know?

The count of letters printed after \(n\) rows is the triangular number \(n(n+1)/2\).

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