Reverse to-A Rows in JavaScript

Beginner
⏱️ 8 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Descending inner loop

What You'll Learn

The first row prints E down to A (EDCBA). Each next row drops the highest letter: DCBA, CBA, BA, then A. The outer loop walks E → A; the inner loop counts j from the current i down to A.

Total letters printed is \(5 + 4 + 3 + 2 + 1 = 15 = n(n+1)/2\) for five rows.

⭐ Pattern Output

Five rows as i moves from E down to A:

Output
EDCBA
DCBA
CBA
BA
A
1

Node.js / console version

Outer loop decreases i from end to start; inner loop runs j from i down to start:

JavaScript
const start = "A".charCodeAt(0);
const end = "E".charCodeAt(0);

for (let i = end; i >= start; i--) {
  let line = "";
  for (let j = i; j >= start; j--) {
    line += String.fromCharCode(j);
  }
  console.log(line);
}
2

Browser version (document.write)

Same logic with ASCII codes 6569. Save as .html or use the live editor:

HTML
<!DOCTYPE html>
<html>
<body>
<script>
for (let i = 69; i >= 65; i--) {
  for (let j = i; j >= 65; j--) {
    document.write(String.fromCharCode(j));
  }
  document.write("<br>");
}
</script>
</body>
</html>

🧠 How It Works

1

Outer loop: high letter of the row

for (let i = end; i >= start; i--) sets the first (largest) character on each row: E, then D, …, finally A.

Descending i
2

Inner loop: down to A

for (let j = i; j >= start; j--) prints backward along the alphabet from i to A.

Reverse i..A
3

Line break

After each row, console.log(line) or document.write("<br>").

New row
=

Not Program 2

Program 2 fixes the left at E and extends right; here the right anchor is A and the left moves. Complexity O(n²).

💡 Tips for Enhancement

Try These

  • Place Program 2 and this page side by side and compare inner loops
  • Contrast with Program 6 (forward suffix to E)
  • Extract a helper rowReverseToA(high, startCode) that returns one line string

Avoid

  • Using j from E down to i — that is Program 2’s growing pattern, not this one
  • document.write after the document has loaded

Key Takeaways

1

Outer loop: E → A; inner loop: i → A (descending).

2

Each row is a reverse run ending at A.

3

Total letters: \(n(n+1)/2\).

4

Same outer direction as Program 2; different inner range.

5

Time complexity: O(n²).

❓ Frequently Asked Questions

No. Program 2 uses j from E down to i, so rows grow: E, ED, EDC, … Program 7 uses j from i down to A, so rows shrink: EDCBA, DCBA, …
Program 6 walks forward from i to E with i increasing. Program 7 walks backward from i to A with i decreasing from E.
O(n²) for n rows from A to E.

Next: JavaScript Alphabet Pattern 8

Continue to the next program in the alphabet pattern series.

Program 8 →
Did you know?

Program 2 and Program 7 both use an outer loop from E down to A. The inner loop is what changes: E → i grows rows left-to-right from E; i → A (descending) shrinks reverse rows ending at A.

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