Spaced Pyramid With a Global Letter Counter

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):
A
B C D
E F G H INode.js / console version
Use Unicode non-breaking spaces so a monospace pre lines up like the HTML output:
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);
}Browser version (document.write)
Matches the reference logic with correct entities inside the strings passed to document.write:
<!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(" ");
} else {
document.write(String.fromCharCode(k++) + " ");
}
}
document.write("<br>");
}
</script>
</body>
</html>🧠 How It Works
Outer loop: i = 65, 67, 69
Step 2 picks row “anchors” at A, C, and E.
Inner loop: j from E to A
Five slots per row. Large j on the left creates left padding when j > i.
if (j > i)
Emit two non-breaking spaces (or \u00A0\u00A0 in Node). Otherwise emit the next letter and the same padding.
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
topand add moreisteps to grow the pyramid
Avoid
- Malformed nbsp entities — browsers will not treat them as spaces
- Resetting
kinside the outer loop if you want one continuous A–I stream
Key Takeaways
Five columns (j from E down to A) and three rows (i += 2).
j > i prints padding; otherwise k supplies the next letter.
Nine letters total: A … I (1 + 3 + 5).
Use proper HTML nbsp entities or \u00A0 so spacing survives layout.
❓ Frequently Asked Questions
j > i rule, together with i at A, C, and E, produces 1, 3, and 5 letters per row.\u00A0 does the same in a string.Next: JavaScript Alphabet Pattern 17
Diagonal * across a full E–A letter scan each row.
The counts 1, 3, 5 are the first three odd numbers; their sum is 9, the ninth letter I.
10 people found this page helpful
