JavaScript Reverse Alphabet Triangle Pattern

Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

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

Remember
Rule: on row i, print i letters from top down

E
ED
EDC
EDCB
EDCBA     ← 5 rows (top = E)

Same staircase shape as Program 1, but descending along the alphabet. The top letter is A + rows - 1 (so 5 rows → E).

How to Solve It

Two ways to emit the same shape — start with nested descending loops, then optionally shorten with slice.

MethodIdeaBest for
Nested loopsOuter = rows; inner counts down from top with code--Learning, interviews, exams
descending.slice(0, i)Take the first i letters of a reverse string like EDCBAShorter demos once loops click

Pseudocode

Pseudocode
top = code of 'A' + rows - 1
for i from 1 to rows:
    line = ""
    for code from top down to top - i + 1:
        append fromCharCode(code) to line
    print line

Cheat sheet

GoalPattern
Walk each rowfor (let i = 1; i <= rows; i++)
Top letter codeconst top = "A".charCodeAt(0) + rows - 1;
Append i descending lettersfor (let code = top; code > top - i; code--) line += String.fromCharCode(code);
End the rowconsole.log(line);
One-line row shortcutconsole.log(descending.slice(0, i));
Forward twinCount up from A → Program 1

Printing Letters vs Starting a New Line

APIEffectUse for
line += chStays on the same rowEach letter
console.log(line)Ends the current rowAfter the inner loop

Append letters without a newline, then end the row once.

Live Preview

Change the row count and the reverse alphabet triangle updates instantly — including the top letter and triangular total.

Whole numbers from 1 to 10. Size 5 starts at E. Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · top E · 15 letters
E
ED
EDC
EDCB
EDCBA

Worked Walkthrough — rows = 4

With 4 rows, top = D. Trace each outer-loop value of i and the letters appended on that row.

iCodesPrinted rowLetters
1DD1
2D..CDC2
3D..BDCB3
4D..ADCBA4

Total letter characters: 1 + 2 + 3 + 4 = 10 = 4×5/2. That triangular sum is why time is O(n²).

JavaScript Programs

Three complete programs: fixed rows, prompt input, and a slice shortcut. Use View Output for sample results, or Try It Yourself to edit and run in the playground.

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

1. Set height and top code. rows = 5; top becomes the code of E.

2. Outer loop picks the row. i runs from 1 to rows. Start each row with an empty line.

3. Inner loop counts down. code runs from top down through i letters, so row i gets exactly i characters.

4. Break the line. console.log(line) after the inner loop prints the row and starts the next one.

When i = 1 you get E; when i = 3 you get EDC; and so on up to EDCBA.

Example 2 — User Input Version

Read the row count at runtime with prompt. Validate with parseInt and clamp to 26 for A–Z demos.

JavaScript
let rows = parseInt(prompt("Enter the number of rows:"), 10);

if (!Number.isFinite(rows) || rows < 1) {
  console.log("Please enter a whole number of rows >= 1.");
} else {
  if (rows > 26) 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

1. Prompt and parse. Ask for a row count, then convert with parseInt(..., 10).

2. Validate and clamp. Reject NaN or non-positive values; cap at 26 so codes stay in A–Z.

3. Same nested-loop core. Recompute top from rows, then print with the same descending inner loop as Example 1.

Example 3 — descending.slice(0, i)

Build a reverse string such as EDCBA, then take the first i letters each row — same shape, no explicit inner letter loop.

JavaScript
const rows = 5;
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const descending = letters.slice(0, rows).split("").reverse().join("");

for (let i = 1; i <= rows; i++) {
  console.log(descending.slice(0, i));
}
Try It Yourself

How It Works

1. Build the reverse string. letters.slice(0, 5) is ABCDE; reversing it gives EDCBA.

2. Slice each prefix. Row i is simply descending.slice(0, i) — same output as the nested descending loops.

Prefer nested code-- loops while learning; use this shortcut once the pattern is clear.

Edge Cases & Pitfalls

Check these before calling the solution done.

wrong direction

Ascending instead of descending

code++ from A prints Program 1. Use code-- from top for this pattern.

empty range

Forgot code--

for (let code = top; code > top - i; code++) never terminates usefully — the step must be --.

log early

One letter per line

Call console.log only after the inner loop finishes. Logging inside splits a row into single characters.

Reuse line

Growing leftovers

Reset line = "" at the start of each outer iteration, or previous letters stick around.

vs Program 3

Different start each row

If rows start at E, D, C… but still end at E (E, DE, CDE), that is Program 3 — not this pattern.

rows = 1

Single A

Output is just A — a good sanity check that top and the descending loop still work.

Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–2)O(n²)O(n) for the current line string
slice shortcut (Example 3)O(n²)O(n) for the descending string + each slice

Total printed characters are 1 + 2 + … + n = n(n+1)/2, so work is quadratic in the number of rows either way.

Key Takeaways

  • Rule: row i prints i letters from top down toward A.
  • Top letter: top = "A".charCodeAt(0) + rows - 1 (5 rows → E).
  • Break the row: call console.log(line) only after the inner loop.
  • Complexity: O(n²) from the triangular letter count n(n+1)/2.

One line: for each row length i, append i letters counting down from top, then console.log.

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 row. console.log(line) ends the row after the inner loop finishes.
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²) where n is the number of rows. Total printed characters equal 1+2+…+n = n(n+1)/2.
Use parseInt(prompt(...), 10), check Number.isFinite(rows), and clamp between 1 and 26 so letter codes stay within A–Z.
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. Build a descending string such as EDCBA and console.log(descending.slice(0, i)) for row length i. Nested loops are better for learning; slicing is a handy shortcut later.

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.

Next: Reverse Starting Letter

Each row starts one letter earlier but still ends at E — E, DE, CDE, BCDE, ABCDE.

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