Centered Pyramid (A to i)

What You'll Learn
This program prints a centered pyramid. For each row, we print some indentation, then print letters from A up to the row letter, separated into cells so spacing stays consistent.
⭐ Pattern Output
A
A B
A B C
A B C D
A B C D ENode.js / console version
Build the line with indentation and letters, then log it.
const A = "A".charCodeAt(0);
const top = "E".charCodeAt(0);
for (let i = A; i <= top; i++) {
let line = "";
for (let j = top; j > i; j--) line += " ";
for (let k = A; k <= i; k++) line += String.fromCharCode(k) + " ";
console.log(line.trimEnd());
}Browser version (document.write + div)
Reference-style output: use for indentation and fixed-width cells for letters.
<!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;
for (let i = A; i <= top; i++) {
for (let j = top; j > i; j--) document.write(" ");
for (let k = A; k <= i; k++) document.write("<div class=\\"cell\\">" + String.fromCharCode(k) + "</div>");
document.write("<br>");
}
</script>
</body>
</html>🧠 How It Works
Core idea
Outer i runs from A to E. First inner loop prints indentation (two spaces per step) while j > i. Second loop prints A..i using String.fromCharCode, producing a centered pyramid.
Loops + condition
An outer loop picks the row, inner loop(s) decide what to print (letter / star / blank) based on comparisons.
Connect it to the output
Compare each loop boundary with the pattern output above — each row corresponds to one outer iteration.
Key Takeaways
Indentation prints before letters to center the pyramid.
Letters print from A to i each row.
Fixed-width cells prevent spacing issues in HTML.
Time complexity: O(n²).
❓ Frequently Asked Questions
or fixed-width cells preserve spacing." " in the Node.js example (and adjust the HTML cell width if needed).top to a later letter (e.g., "H".charCodeAt(0) / 72) and the loops will scale.More JavaScript alphabet patterns
Browse the full list for more variations.
The indentation loop runs \(top - i\) times, so the pyramid shifts left one step each row.
10 people found this page helpful
