Reverse Alphabet Right-Angled Triangle Pattern in JavaScript

Beginner
⏱️ 8 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Nested Loops

What You’ll Learn

Print a reverse alphabet right-angled triangle: each row has one more character than the previous, and letters go from a top letter down toward AE, ED, EDC, EDCB, EDCBA. Same geometry as Program 1, but descending along the alphabet. Includes a live preview, worked JavaScript examples, edge cases, and complexity.

Shape Rule

Growing reverse rows

Row i prints i letters from top down.

Outer Loop

Row length

for (let i = 1; i <= rows; i++) picks how many letters each row prints.

Inner Loop

Always from top

for (let code = top; code > top - i; code--) appends descending codes from top.

charCodeAt / fromCharCode

Letter codes

top = "A".charCodeAt(0) + rows - 1 then String.fromCharCode(code) for output.

Live Preview

1–10 rows

Pick a row count and draw the reverse triangle instantly in the browser.

O(n²)

Complexity

Triangular letter count: n(n+1)/2 prints.

Introduction

A reverse alphabet right-angled triangle grows like Program 1, but every row starts at a fixed top letter and counts downward until a row-specific end letter.

In JavaScript you solve it with nested for loops and a descending inner counter: the outer loop picks the row length, the inner loop appends letter codes from top down, then console.log(line) moves to the next line.

Why it matters?

It locks in reverse iteration with range step -1 — the same skill used in reverse triangles, diagonals, and mirrored alphabet labs.

Key Highlights

Growing Rows

1, 2, 3, … letters per row.

Always From Top

Inner loop restarts at the top letter.

Descending Letters

for (let code = top; code > top - i; code--) counts down.

Mirror of Program 1

Same triangle; opposite letter direction.

In short: for each row i from 1 to rows, append top..top-i+1 with line += String.fromCharCode(code), then call console.log(line).

📝 Problem & Approach

Given a positive integer rows, print a left-aligned reverse alphabet right-angled triangle of letters with rows lines.

JavaScript
# First 5 rows (conceptual shape)
# E
# ED
# EDC
# EDCB
# EDCBA

Inputs & Outputs

ItemTypeDescription
rowsintNumber of triangle lines to print (typically ≥ 1).
topint (code)Top letter code: "A".charCodeAt(0) + rows - 1.
Printed outputtextGrowing reverse prefixes from top down to A on the last row.

Minimal workflow

Pseudocode
top = "A".charCodeAt(0) + rows - 1
for i from 1 to rows:
    line = ""
    for code from top down to top - i + 1:
        line += letter
    console.log(line)

Approach comparison

ApproachIdeaBest for
code--Outer row length + inner descending codesLearning and interviews
Char outer loopWalk end letter from top down to AMatching classic E…EDCBA samples

⚡ Quick Reference

GoalPattern
Top lettertop = "A".charCodeAt(0) + rows - 1
Walk each rowfor (let i = 1; i <= rows; i++)
Print descendingfor (let code = top; code > top - i; code--) line += String.fromCharCode(code)
End the rowconsole.log(line)
Forward triangleSee Program 1
LowercaseUse "a".charCodeAt(0) as the base instead of "A".charCodeAt(0)

📋 line += vs console.log vs Direction

Same triangle idea as Program 1 — only letter direction changes.

line += letter
letter

Prints each descending letter on the current row

console.log
break

Ends the row after top..end finishes

Program 2
top..down

Inner loop uses code--

Program 1
A..end

Inner loop counts up from A

Context

When This Pattern Shows Up

Reach for this when teaching reverse character loops on a growing triangle.

  1. Right after Program 1

    Keep the triangle; flip letter direction to descending.

  2. Reverse range drills

    Practice for (let code = top; code > top - i; code--) and stopping at top - i + 1.

  3. Before Program 3

    Next you change only the starting letter while counting forward.

  4. Top-letter math

    Practice top = "A".charCodeAt(0) + rows - 1 for any height.

  5. Not a UI layout tool

    This is a console teaching pattern — not how you build modern app screens.

Key benefit: one bound change (descending code--) turns a forward triangle into a reverse one.

🔮 Live Preview

Choose between 1 and 10 rows and draw the reverse alphabet triangle in the browser.

Try 5 (classic E…EDCBA) or 4 (D…DCBA). Max 10 keeps the preview readable.

Live result
Press "Draw pattern".

Examples Gallery

Three complete JavaScript programs - fixed row count, prompt input, and a spaced-letter variant. Click View Output to reveal sample console results, or Try it Yourself to run in the browser editor.

📚 Getting Started

Print five reverse rows with nested loops and a descending inner counter.

Example 1 — Fixed rows = 5

Hard-coded height - ideal for first demos and screenshots.

JavaScript
const rows = 5;
const top = "A".charCodeAt(0) + rows - 1;

for (let i = 1; i <= rows; i++) {
  let line = "";
  for (let code = top; code > top - i; code--) {
    line += String.fromCharCode(code);
  }
  console.log(line);
}
Try it Yourself

How It Works

When i = 1, the inner loop appends E. When i = 3, it appends EDC, and so on through five letters on the last row. console.log(line) after the inner loop starts the next row.

📈 Practical Variant

Let the user choose the height at runtime.

Example 2 — User Input Version

Read the row count with prompt() and clamp with Math.max(1, Math.min(rows, 26)). Validate with Number.isFinite in real apps.

JavaScript
let rows = parseInt(prompt("Enter the number of rows:"), 10);
if (!Number.isFinite(rows)) rows = 5;
rows = Math.max(1, Math.min(rows, 26));
const top = "A".charCodeAt(0) + rows - 1;

for (let i = 1; i <= rows; i++) {
  let line = "";
  for (let code = top; code > top - i; code--) {
    line += String.fromCharCode(code);
  }
  console.log(line);
}
Try it Yourself

How It Works

Same charCodeAt/fromCharCode core as Example 1; only the source of rows changes. For 4 rows, top becomes "D".charCodeAt(0). Non-numeric input yields NaN with bare parseInt(prompt()) - check Number.isFinite for safer labs.

⚡ Readability Variant

Same reverse triangle with spaces between letters.

Example 3 — Spaced Letters

Append a trailing space after each letter so columns are easier to scan.

JavaScript
const rows = 5;
const top = "A".charCodeAt(0) + rows - 1;

for (let i = 1; i <= rows; i++) {
  let line = "";
  for (let code = top; code > top - i; code--) {
    line += String.fromCharCode(code) + " ";
  }
  console.log(line);
}
Try it Yourself

How It Works

Loop bounds are unchanged - only the appended unit becomes String.fromCharCode(code) + " ". Trim trailing spaces later if you need a compact line.

🧠 How the Algorithm Prints Rows

1

Set up

Set rows (fixed or from prompt()). Compute top = "A".charCodeAt(0) + rows - 1.

Setup
2

Outer loop (rows)

for (let i = 1; i <= rows; i++) selects how many letters print on the current line.

Row
3

Inner loop (descending)

for (let code = top; code > top - i; code--) appends each letter with line += String.fromCharCode(code).

Letters
4

New line

console.log(line) ends the row so the next outer iteration starts fresh.

Break
=

Reverse letter triangle complete

Total letters: 1+2+…+n = n(n+1)/2O(n²) time, O(1) extra memory.

🔎 Worked Walkthrough — rows = 5

Trace each outer value of i and the descending codes the inner loop prints from top = E.

Row iInner rangePrinted rowLetters this row
1E..DE1
2E..CED2
3E..BEDC3
4E..AEDCB4
5E..A (5 letters)EDCBA5

*range stops before the end value, so top - 5 is below A and all five letters print. Total letter prints: 1 + 2 + 3 + 4 + 5 = 15 = 5×6/2.

Use Cases

Where this reverse triangle (and its descending loops) shows up beyond the homework prompt.

1. Direction Practice

Clearest alphabet demo of counting letters downward with code--.

Example: flip bounds to Program 1 and compare.

2. Pair with Program 1

Same triangle geometry — forward vs reverse fill.

Example: print both side by side for n = 5.

3. Top-Letter Labs

Practice computing top from a row count.

Example: rows 1..10 map to A..J.

4. Spaced Output

Add separators without changing loop structure (Example 3).

Example: print String.fromCharCode(code) + " " for readable columns.

5. Complexity Intuition

Triangular sums make O(n²) easy to see.

Example: 5 rows print 15 letters total.

6. Input Validation Labs

Pair the pattern with Number.isFinite and clamp to 26.

Example: reject rows > 26 or clamp it.

Pro Tip: say “always start at top, print down for i letters” before coding — that story prevents wrong inner bounds.

Advantages

Why this pattern earns a spot right after the forward alphabet triangle.

  1. 1. Instant Visual Feedback

    Wrong direction or bounds show up immediately as a non-reverse triangle.

  2. 2. Tiny Change from Program 1

    Same structure; only loop direction and range step flip.

  3. 3. Teaches Descending Loops

    for (let code = top; code > top - i; code--) is reusable in many JavaScript patterns.

  4. 4. O(1) Extra Memory

    Streaming output needs no storage beyond loop counters.

Pro Tip: master Program 1 first; treat this page as the same story with arrows reversed.

Usage Tips

Small habits that keep reverse-triangle code clean.

  1. 1. Restart Inner at Top

    Every row starts from the same top letter; only the count changes with i.

  2. 2. Use for (let code = top; code > top - i; code--)

    That triple is what produces E, ED, EDC, …

  3. 3. Validate parseInt(prompt()) with Number.isFinite

    Avoid crashes when the user types letters instead of a number.

  4. 4. Cap at 26 Rows

    Beyond Z you need a wrap/stop policy for top.

  5. 5. Dry-Run Row 3

    Trace EDC on paper before coding larger n.

Pro Tip: if every row starts with a different letter and runs forward to E, you wrote Program 3 — not this pattern.

Common Pitfalls

Mistakes that commonly break reverse alphabet triangles.

  1. 1. Using Forward Inner Bounds

    for (let code = start; code < start + i; code++) prints Program 1 instead.

    → Use for (let code = top; code > top - i; code--).

  2. 2. Forgetting Step -1

    for (let code = top; code > top - i; code++) with default step +1 produces an empty range.

    → Always pass -1 as the third argument.

  3. 3. console.log(line) Inside the Inner Loop

    Each letter lands on its own line — you get a column, not a triangle.

    → Use line += ... for letters; console.log(line) only after the inner loop.

  4. 4. Blind parseInt(prompt())

    Non-numeric input yields NaN with bare parseInt(prompt()).

    → Wrap in Number.isFinite and validate range.

  5. 5. Overflowing Z

    Large rows makes top walk past Z.

    → Cap input at 26 or define a wrap policy.

Edge Cases

Check these inputs before calling the solution done.

rows = 1

Single letter

Output is just A on one line.

rows = 5

Classic sample

Through EDCBA.

rows = 4

Shorter triangle

Top is D → DDCBA.

rows > 26

Past Z

Reject, clamp, or wrap — decide explicitly.

Bad input

Non-numeric input

parseInt(prompt()) yields NaN — validate first.

Case

Lowercase

Same loops with "a".charCodeAt(0) as the base.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Flip to forward

2. Add spaces

  • Print String.fromCharCode(code) + " " (Example 3)
  • Keep the same loop bounds

3. Change only the start

4. Star triangle twin

Notes

  • Same top every row. Only how many letters print changes with i.
  • Total letters for n rows is the triangular number n(n+1)/2.
  • Compute top = "A".charCodeAt(0) + rows - 1 to generalize any height.
  • This is the descending mirror of Program 1’s forward triangle.

Quick Takeaway: start every row at the top letter, print down for i letters, then break the line — that is the whole triangle.

⏱️ Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(rows²)O(1)
Spaced letters (Example 3)O(rows²)O(1)

Row k prints k letters; summing 1..n gives n(n+1)/2 character writes.

Wrap Up

🎉 Conclusion

The reverse alphabet right-angled triangle is a small nested-loop exercise with lasting payoff: fixed top letter, descending inner walk with code--, and growing row length. Master the classic E…EDCBA sample, then try user input and optional spacing.

Practice the three examples above, then continue to Program 3’s triangle where each row starts one letter earlier but still runs forward.

Compute a top letter, print top..top-i+1 on each row, and break only after the inner loop finishes.

💡 Best Practices

✅ Do

  • Restart the inner loop at top every row
  • Use for (let code = top; code > top - i; code--) for descending output
  • Compute top = "A".charCodeAt(0) + rows - 1
  • Validate parseInt(prompt()) with Number.isFinite and cap at 26
  • State O(n²) when asked about complexity

❌ Don’t

  • Use forward for (let code = start; code < start + i; code++) bounds for this pattern
  • Forget the -1 step in the inner range
  • Call console.log(line) inside the inner letter loop
  • Let rows exceed 26 without a policy
  • Confuse this with Program 3’s changing start letter

Key Takeaways

Knowledge Unlocked

Five things to remember about this alphabet pattern

Print the reverse alphabet right-angled triangle the beginner-friendly way.

5
Core concepts
T 02

Top

Inner always starts here

Code
-1 03

range step

Counts down to top-i

Code
04

console.log

Ends each row

I/O
O 05

Complexity

O(n²) time

Analysis

❓ Frequently Asked Questions

The outer loop picks row length i from 1 to rows. The inner loop runs for (let code = top; code > top - i; code--) to append top down through i letters - so row 1 is E, row 2 is ED, and so on.
The inner loop always starts at top (E when rows = 5). Only how many letters print changes with i - that is what grows the reverse triangle.
line += String.fromCharCode(code) stays on the same conceptual row. console.log(line) ends the current row. Append letters first; log once after the inner loop.
Use Program 1: loop upward from A with for (let code = start; code < start + i; code++). This page is the descending mirror of that forward triangle.
O(n^2) where n is the number of rows. Total printed characters equal 1+2+...+n = n(n+1)/2.
Use parseInt with Number.isFinite after prompt(), then clamp rows with Math.max(1, Math.min(rows, 26)) so bad input does not walk past Z.
The code-- counts down. The loop stops when code is no longer greater than top - i, so you get exactly i values: top, top-1, ..., top-i+1.
Yes. Use 'a'.charCodeAt(0) as the base: top = 'a'.charCodeAt(0) + rows - 1, then keep the same nested descending loops.

Did you Know? 🔊

Row i prints i letters from the top letter down. For 5 rows the output is E, ED, EDC, EDCB, EDCBA - the descending mirror of Program 1. Total letters = n(n+1)/2.

Continue to Alphabet Pattern 3

Next up: each row starts one letter earlier, but letters still run forward to the top.

Program 3 tutorial →

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.

12 people found this page helpful