Spaced Pyramid With a Global Letter Counter

Beginner
⏱️ 9 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
i += 2, k never reset

What You'll Learn

This pattern uses five fixed columns (mapped to j = 69, 68, …, 65) and only three outer rows: i is 65, 67, 69. Where j > i, the code prints padding; otherwise it prints the next letter from a single counter k starting at 65 (A). Because k is not reset between rows, the letters flow A, then B C D, then E F G H I — nine characters in order, similar in spirit to Program 13.

Some older tutorials used broken non-breaking-space markup (for example   instead of  ); in plain JavaScript strings you can use \u00A0 instead.

⭐ Pattern Output

As in the original (non-breaking spaces keep columns aligned in the browser):

Output
    A  
  B C D  
E F G H I
1

Node.js / console version

Use Unicode non-breaking spaces so a monospace pre lines up like the HTML output:

JavaScript
const A = "A".charCodeAt(0);
const top = "E".charCodeAt(0); // 69
let k = A;
const pad = "\u00A0\u00A0";

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

Browser version (document.write)

Matches the reference logic with correct &nbsp; entities inside the strings passed to document.write:

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

🧠 How It Works

1

Outer loop: i = 65, 67, 69

Step 2 picks row “anchors” at A, C, and E.

3 rows
2

Inner loop: j from E to A

Five slots per row. Large j on the left creates left padding when j > i.

Columns
3

if (j > i)

Emit two non-breaking spaces (or \u00A0\u00A0 in Node). Otherwise emit the next letter and the same padding.

Shape
=

Global k++

Letters do not restart each row: you get 1 + 3 + 5 = 9 characters, A through I.

💡 Tips for Enhancement

Try These

  • Replace padding with plain spaces in the console to see how alignment changes
  • Compare with Program 14 (step-2 outer loop without column scan)
  • Extend top and add more i steps to grow the pyramid

Avoid

  • Malformed nbsp entities — browsers will not treat them as spaces
  • Resetting k inside the outer loop if you want one continuous A–I stream

Key Takeaways

1

Five columns (j from E down to A) and three rows (i += 2).

2

j > i prints padding; otherwise k supplies the next letter.

3

Nine letters total: AI (1 + 3 + 5).

4

Use proper HTML nbsp entities or \u00A0 so spacing survives layout.

❓ Frequently Asked Questions

The pattern is designed for three rows only. The j > i rule, together with i at A, C, and E, produces 1, 3, and 5 letters per row.
In HTML, consecutive normal spaces often collapse. Non-breaking spaces keep the grid stable; in Node, \u00A0 does the same in a string.
Here, three outer iterations each run five inner iterations: O(15) for fixed size. In general, O(rows × columns).

Next: JavaScript Alphabet Pattern 17

Diagonal * across a full EA letter scan each row.

Program 17 →
Did you know?

The counts 1, 3, 5 are the first three odd numbers; their sum is 9, the ninth letter I.

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