Reverse Pyramid (Right-aligned)

What You'll Learn
This pattern prints a right-aligned triangle where each row starts from E and goes down to the row letter. We use one loop to print leading blanks and another loop to print letters in descending order.
⭐ Pattern Output
E
E D
E D C
E D C B
E D C B ANode.js / console version
Build each row with blanks and letters, then log it.
const A = "A".charCodeAt(0);
const top = "E".charCodeAt(0);
for (let i = top; i >= A; i--) {
let line = "";
for (let j = A; j < i; j++) line += " ";
for (let j = top; j >= i; j--) line += String.fromCharCode(j) + " ";
console.log(line.trimEnd());
}Browser version (document.write + div)
Matches the reference: fixed-width cells for alignment.
<!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 = top; i >= A; i--) {
for (let j = A; j < i; j++) document.write(\"<div class=\\\"cell\\\"></div>\");
for (let j = top; j >= i; j--) document.write(\"<div class=\\\"cell\\\">\" + String.fromCharCode(j) + \"</div>\");
document.write(\"<br>\");
}
</script>
</body>
</html>🧠 How It Works
Outer loop: i from E down to A
Each next row shifts right and prints one more letter.
Indent: j from A to i - 1
Print two-space (or empty cell) padding so lower rows start further right.
Letters: j from E down to i
Print the descending run E, D, …, i with a space after each character.
Right-shifting triangle
Padding grows while the printed run expands, producing a slanted triangle.
Key Takeaways
Outer loop counts down from E to A.
First inner loop prints blanks to right-align.
Second inner loop prints E..i in reverse.
Time complexity: O(n²).
❓ Frequently Asked Questions
const A = "a".charCodeAt(0) and const top = "e".charCodeAt(0).Next up
Continue with the next alphabet program.
Using fixed-width cells in HTML is often simpler than mixing spaces and when building aligned patterns.
10 people found this page helpful
