Reverse-to-A Alphabet Rows in JavaScript

What You'll Learn
The outer loop walks A through E. On each row, the inner loop prints from that letter backward to A, so the first row is just A, then BA, then CBA, and so on.
Total letters printed is \(1 + 2 + \cdots + n = \frac{n(n+1)}{2}\) for n rows.
⭐ Pattern Output
For five rows (A–E):
A
BA
CBA
DCBA
EDCBANode.js / console version
Outer loop increases i; inner loop decreases j from i to start:
const start = "A".charCodeAt(0);
const end = "E".charCodeAt(0);
for (let i = start; i <= end; i++) {
let line = "";
for (let j = i; j >= start; j--) {
line += String.fromCharCode(j);
}
console.log(line);
}Browser version (document.write)
Same logic with numeric codes 65–69. Save as .html or use the live editor:
<!DOCTYPE html>
<html>
<body>
<script>
for (let i = 65; i <= 69; i++) {
for (let j = i; j >= 65; j--) {
document.write(String.fromCharCode(j));
}
document.write("<br>");
}
</script>
</body>
</html>🧠 How It Works
Outer loop: grow the peak letter
for (let i = start; i <= end; i++) picks the highest letter on the row: A, then B, …, up to E.
iInner loop: walk down to A
for (let j = i; j >= start; j--) prints i, then i-1, …, until A.
jLine break
After each row, console.log(line) or document.write("<br>").
Reverse runs
Same row lengths as Program 1, but each row reads right-to-left in alphabetical order. Complexity O(n²) for n rows.
💡 Tips for Enhancement
Try These
- Compare with inverted right-aligned stars (similar widening idea)
- Build each row with
[...]and.reverse().join("")for practice - Re-read Program 1 to contrast forward vs reverse rows
Avoid
- Using
j++by mistake in the inner loop (you needj--) document.writeafter the document has loaded
Key Takeaways
Outer loop goes A → E; inner loop goes i → A.
Each row ends with A; the row length equals the row index offset from A plus one.
Total letters: \(n(n+1)/2\).
This is a descending-letter variant of the right triangle family.
Time complexity: O(n²).
❓ Frequently Asked Questions
AB, ABC, …). Here the goal is explicitly BA, CBA, … by decrementing j.i === start, the inner loop runs only once at j === 65, which is A.n rows, same as other triangle alphabet patterns on this site.Next: JavaScript Alphabet Pattern 5
Continue to the next program in the alphabet pattern series.
If you reverse the string of each row from Program 1, you obtain the rows of this pattern for the same letter range.
10 people found this page helpful
