Reverse Alphabet Pattern in JavaScript

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

What You’ll Learn

The reverse alphabet pattern prints descending letters on each row, from a fixed top letter down to a row-specific stop. This tutorial covers the shape rule, fixed top formula, reverse range step, a live preview, algorithm steps, worked JavaScript examples, edge cases, and complexity.

Shape Rule

Fixed top, shifting stop

Row 0 prints EDCBA, row 1 prints DCBA, row 2 prints CBA, down to a single A on the last row.

Outer Loop

Row index

for (let i = 0; i < rows; i++) picks the stop letter for each row — A on row 0, B on row 1, up to the top letter on the last row.

Inner Loop

top down to stop

for (let code = top; code >= stop; code--) appends descending letters from the fixed top down to the row stop.

line vs console.log

Same line / next line

Letters use line += String.fromCharCode(code); end each row with console.log(line).

Live Preview

1–26 rows

Pick a row count and draw the fixed-top reverse alphabet pattern instantly in the browser.

O(n²)

Complexity

Total letters = n(n+1)/2; extra memory stays O(1).

Introduction

A reverse alphabet pattern (EDCBA to E) prints descending letters on each row — every row starts at the same top letter while the stop moves up each line. With five rows the console shows EDCBA, EDCB, EDC, ED, E — the fixed-top mirror of Program 7’s shrinking-start shape.

in JavaScript you solve it with two nested for loops: compute top = "A".charCodeAt(0) + rows - 1, set stop = "A".charCodeAt(0) + i per row, print letters with for (let code = top; code >= stop; code--), then call console.log(line) for the next line.

Why it matters?

It teaches per-row descending bounds with a fixed start at the top letter — the natural follow-up after Program 7. Once stop = base + i and for (let code = top; code >= stop; code--) click, slice shortcuts and Program 9 follow naturally.

Key Highlights

Fixed Top Letter

top = "A".charCodeAt(0) + rows - 1 — for five rows, every row starts at E.

Shifting Stop

Row i stops at String.fromCharCode(base + i) — A, then B, then C, up to the top letter on the last row.

Descending loop

for (let code = top; code >= stop; code--) counts down; line += String.fromCharCode(code) then console.log(line).

Program 7 Contrast

Program 7 shrinks EDCBA, DCBA, CBA; this pattern keeps E on the left and shifts the stop — compare both side by side.

In short: for each row i from 0 to rows - 1, set stop = "A".charCodeAt(0) + i, print letters from fixed top down to stop with for (let code = top; code >= stop; code--) and line += String.fromCharCode(code), then call console.log(line).

📝 Problem & Approach

Given a positive integer rows, print a left-aligned reverse alphabet pattern: each row prints descending letters from a fixed top letter down to a row-specific stop (EDCBA when rows = 5).

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

Inputs & Outputs

ItemTypeDescription
rowsintNumber of pattern lines to print (typically ≥ 1).
topint (code)Fixed start letter on every row: "A".charCodeAt(0) + rows - 1.
Printed outputtextLeft-aligned rows; row i logs from fixed top down to String.fromCharCode(base + i).

Minimal workflow

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

Approach comparison

ApproachIdeaBest for
Nested reverse loopsFixed top + shifting stopLearning and interviews
Fixed top formulatop = "A".charCodeAt(0) + rows - 1This pattern — shared first letter every row
letters.slice(i, rows).split("").reverse().join("")Slice from row index to top, then reverseShorter production-style demos

⚡ Quick Reference

GoalPattern
Fixed top lettertop = "A".charCodeAt(0) + rows - 1
Walk each rowfor (let i = 0; i < rows; i++)
Row stop letterstop = base + i
Print top down to stopfor (let code = top; code >= stop; code--): line += String.fromCharCode(code)
End the rowconsole.log(line)
One-line row shortcutconsole.log(letters.slice(i, rows).split("").reverse().join(""))
Shifting stop variantSee Program 7 — rows end at A

📋 Nested reverse loop vs slice reverse

Same EDCBA-to-E shape — two ways to think about descending row bounds.

Nested reverse
for (let code = top; code >= stop; code--)

Classic charCode loop — teaches fixed top, shifting stop, and code--

Slice reverse
letters.slice(i, rows).split("").reverse().join("")

Prefix slice then reverse — compact one-liner per row

Learning tip
loops first

Master nested reverse loops before the string shortcut

Context

When This Pattern Shows Up

Reach for this pattern when teaching descending letter bounds with a fixed top letter — the natural follow-up after Program 7’s shrinking-start shape.

  1. Reverse range practice

    Natural follow-up after Program 6 — same row count, every row starts at the top letter while the stop shifts up.

  2. Nested-loop warm-up

    Practice for (let code = top; code >= stop; code--) with an immediate visual check.

  3. prompt() input practice

    Combine loops with prompt() for a flexible row count.

  4. Gateway to variants

    Leads to reverse patterns, pyramids, and hollow shapes in the series.

  5. Not a UI layout tool

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

Key benefit: one small program that locks in a fixed top letter, a rising floor, reverse range step, output sequencing, and O(n²) thinking — the fixed-start step after Program 7.

🔮 Live Preview

Choose a row count between 1 and 26 and draw the fixed-top reverse alphabet pattern in the browser.

Try 5, 7, or 10. Cap is 26 letters (A–Z).

Live result
Press "Draw pattern".

Examples Gallery

Three complete JavaScript programs — fixed row count, prompt input, and a letters.slice(i, rows) reverse shortcut. Click View Output to reveal sample console results, or Try it Yourself to run in the browser editor.

📚 Getting Started

Print five rows with classic nested reverse loops — fixed top, shifting stop.

Example 1 — Fixed rows = 5

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

JavaScript
const rows = 5;

const base = "A".charCodeAt(0);
const top = base + rows - 1; // 'E' when rows = 5

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

How It Works

When i = 0, stop is A and the inner loop prints EDCBA. When i = 2, stop is C and the row is EDC. When i = 4, stop is E, so the last row is a single E. 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 convert with parseInt() (validate with Number.isFinite in real apps).

JavaScript
const rowsInput = prompt("Enter the number of rows (max 26):");
let rows = parseInt(rowsInput, 10);
rows = Math.max(1, Math.min(rows, 26));

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

for (let i = 0; i < rows; i++) {
  const stop = base + i;
  let line = "";
  for (let code = top; code >= stop; 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. The clamp keeps letter codes within A–Z. Non-numeric input returns NaN with bare parseInt(prompt()) — validate with Number.isFinite for safer labs.

⚡ Shortcut Style

Same shape without an explicit inner letter loop.

Example 3 — letters.slice(i, rows) reversed

Slice from row index i to the top letter, then reverse for each row.

JavaScript
const rows = 5;
const clampedRows = Math.max(1, Math.min(rows, 26));
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

for (let i = 0; i < clampedRows; i++) {
  console.log(letters.slice(i, clampedRows).split("").reverse().join(""));
}
Try it Yourself

How It Works

letters.slice(i, rows) returns letters from index i up to index rows. With rows = 5, row 0 is letters.slice(0, 5).split("").reverse().join("") = EDCBA, row 2 is letters.slice(2, 5).split("").reverse().join("") = EDC, and so on. Keep the two-loop version for exams that ask you to show reverse bounds and code--.

🧠 How the Algorithm Prints Rows

1

Set up

Use prompt() when reading input. Set rows (fixed or from user), clamp to 1–26, and compute top = "A".charCodeAt(0) + rows - 1.

Setup
2

Outer loop (row index)

for (let i = 0; i < rows; i++) selects the stop letter for the current line — A on row 0, B on row 1, up to the top letter on the last row.

Row
3

Inner loop (descending letters)

stop = base + i then for (let code = top; code >= stop; 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
=

Fixed-top reverse alphabet pattern complete

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

🔎 Worked Walkthrough — rows = 5

Trace each outer-loop value i (0-based) and see what the inner loop prints from fixed top down to row stop.

Outer itopstopInner loopPrinted rowLetters this row
0EAfor (code = 69; code >= 65; code--)EDCBA5
1EBfor (code = 69; code >= 66; code--)EDCB4
2ECfor (code = 69; code >= 67; code--)EDC3
3EDfor (code = 69; code >= 68; code--)ED2
4EEfor (code = 69; code >= 69; code--)E1

Total letter prints: 5 + 4 + 3 + 2 + 1 = 15 = 5×6/2. Same triangular total as Programs 1, 4, 5, and 7 — only the letter bounds per row differ.

Use Cases

Where this fixed-top reverse descending letter pattern (and its shifting stop) shows up beyond the homework prompt.

1. Teaching Reverse range Bounds

Clearest visual proof that for (let code = top; code >= stop; code--) counts down from a fixed top letter while stop moves forward each row.

Example: compare side-by-side with Program 7.

2. Pattern Series Bridge

Natural step after Program 7 before Program 9’s repeating-letter variant.

Example: Program 9 prints A, BB, CCC, and so on.

3. Console Formatting Drills

Practice reverse character loops and line += String.fromCharCode(code)/console.log(line) with a shape that differs visibly from Program 6 and 7.

Example: compare Program 6 ascending vs Program 7 shrinking-start vs this fixed-top shape.

4. Character Substitution

Swap to lowercase or digits once the letter loop works.

Example: print lowercase a..z once uppercase clicks.

5. Complexity Intuition

Triangular totals make O(n²) concrete for beginners.

Example: count printed letters for n = 10 → 55.

6. Input Validation Labs

Pair the pattern with Number.isFinite validation and positive-row checks.

Example: reject rows <= 0 and re-prompt.

Pro Tip: when an interviewer asks for descending letters per row, explain that stop = base + i and the inner loop uses step -1 down to A.

Advantages

Why this reverse descending pattern earns a spot after Program 7 in beginner JavaScript courses.

  1. 1. Instant Visual Contrast

    Side-by-side with Program 7 makes fixed top vs shrinking start obvious.

  2. 2. Minimal Concepts

    Only loops and console output — no arrays or math libraries.

  3. 3. Easy to Compare

    One formula change flips between Program 7’s shrinking start and this shifting stop shape.

  4. 4. O(1) Extra Memory

    Streaming output needs no storage beyond loop counters.

Pro Tip: master Program 7 first, then this page — the row count is the same; only whether the start or stop moves changes the shape.

Usage Tips

Small habits that keep reverse alphabet-pattern code clean.

  1. 1. Compute top Once

    Set top = "A".charCodeAt(0) + rows - 1 before the outer loop — don’t recalculate every row. Set stop = base + i inside the outer loop.

  2. 2. Validate prompt() input

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

  3. 3. Keep console.log(line) Outside

    Only call console.log(line) after the inner loop finishes the row.

  4. 4. Remember code--

    for (let code = top; code >= stop; code--) needs step -1 and stop stop - 1 so the row stop letter is included.

  5. 5. Dry-Run One Small n

    Trace rows = 3 on paper — expect CBA, CB, C — before coding larger demos.

Pro Tip: if rows print in ascending order, you almost certainly forgot step -1 in the inner range.

Common Pitfalls

Mistakes that commonly break fixed-top reverse alphabet patterns.

  1. 1. Forgetting code-- in inner loop

    Counting up with code++ logs ascending letters — the pattern needs descending order from fixed top.

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

  2. 2. Stopping before row stop letter

    Using code > stop skips the row stop letter — the last character on each row is missing.

    → Use code >= stop so the stop letter is included.

  3. 3. Forgetting the Row Break

    Omitting console.log(line) after the inner loop glues every letter onto one endless line.

    → Always end the row after the inner loop.

  4. 4. Blind parseInt(prompt())

    Non-numeric input raises ValueError with bare parseInt(prompt()).

    → Wrap in Number.isFinite validation and validate range.

  5. 5. Confusing with Program 7 or Program 6

    Program 7 shrinks the start each row but ends at A (EDCBA, DCBA). Program 6 prints ascending rows ending at a fixed top letter — not the same as EDCBA-to-E.

    → This pattern: fixed top, stop = base + i, for (let code = top; code >= stop; code--), every row starts at the top letter.

Edge Cases

Check these inputs before calling the solution done.

rows = 1

Single letter row

Output is just the top letter — stop equals top and the inner loop prints one letter.

rows = 0

Empty pattern

Outer loop never runs — print nothing or show a message.

Negative

rows < 0

Treat as invalid; re-prompt instead of silent empty output.

Large n

Many rows

Output grows as n²/2 characters — fine for labs, noisy for huge n.

Bad input

Non-numeric input

parseInt(prompt()) raises ValueError — validate first.

Last row

start equals A

On the last row, stop == top — inner loop prints one letter only.

🎯 Practice Problems

Try these variations to lock in the fixed-top reverse pattern.

1. Compare with Program 7

  • Run both patterns with the same rows
  • Note shrinking start vs fixed top

2. Print digits instead

  • Replace String.fromCharCode(code) with digit logic
  • Same reverse descending structure

3. Safe input loop

  • Use Number.isFinite validation until rows >= 1
  • Then draw the reverse pattern

4. Continue the series

  • Try Program 9 — alphabet pattern A, BB, CCC, ...
  • Next step after EDCBA-to-E

Notes

  • Fixed top. top = "A".charCodeAt(0) + rows - 1 is computed once — for five rows every row starts at E.
  • for (let code = top; code >= stop; code--) needs step -1 and stop stop - 1 so the row stop letter is included.
  • Validate rows > 0 for interactive programs; rows = 1 should print a single top letter.
  • Program 7 ends at A; Program 6 ascends to a fixed top — compare both to see how bounds drive the shape.

Quick Takeaway: compute fixed top, shift stop each row, print with for (let code = top; code >= stop; code--), then break the line.

⏱️ Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–2)O(rows²)O(1)
letters.slice(i, rows).split("").reverse().join("") (Example 3)O(rows²)O(rows) per row string (temporary)
Wrap Up

🎉 Conclusion

The reverse alphabet pattern (EDCBA to E) is a compact bounds exercise with lasting payoff: fixed top, fixed top, per-row shifting stop, reverse range, and O(n²) intuition. Master the classic two-loop version, then optionally shorten rows with letters.slice(i, rows).split("").reverse().join("").

Practice the three examples above, then continue to Program 9 for the repeating-letter variant in the series.

Every row starts at the top letter — use for (let code = top; code >= stop; code--), build with line += String.fromCharCode(code), log with console.log(line), and validate row counts when reading from prompt().

💡 Best Practices

✅ Do

  • Compute top = "A".charCodeAt(0) + rows - 1 once before the outer loop
  • Use stop = base + i inside for (let i = 0; i < rows; i++)
  • Use for (let code = top; code >= stop; code--) and line += String.fromCharCode(code)
  • Validate rows ≥ 1 for interactive programs
  • Wrap parseInt(prompt()) in Number.isFinite validation
  • State O(n²) time when asked about complexity

❌ Don’t

  • Forget step -1 on the inner range
  • Use Program 7’s shrinking-start logic for this shape
  • Confuse this page with Program 7’s EDCBA-to-A variant
  • Skip the newline after each row
  • Ignore bad console input in user-facing demos
  • Skip the rows = 1 edge case

Key Takeaways

Knowledge Unlocked

Five things to remember about this fixed-top reverse pattern

Print EDCBA-to-E the beginner-friendly way.

5
Core concepts
E 02

Fixed top

top = base + rows - 1

Code
03

Shifting stop

stop = base + i

Code
04

code-- loop

for (let code = top; code >= stop; code--)

for (let code = top; code >= stop; code--)

I/O
O 05

Complexity

O(n²) time

Analysis

❓ Frequently Asked Questions

The outer loop walks i from 0 to rows-1. Each row keeps top = "A".charCodeAt(0) + rows - 1 as the start letter and sets stop = "A".charCodeAt(0) + i. The inner loop uses for (let code = top; code >= stop; code--) to append descending letters down to the row stop, so row 0 is EDCBA, row 2 is EDC, and the last row is E.
For rows = 5, top is the code for E — the first letter on every row. Unlike Program 7, the start letter stays fixed while the stop letter moves up each row.
Descending letters need for (let code = top; code >= stop; code--). Stopping early or counting up logs ascending letters or skips the last letter on the row.
Program 7 shrinks the start each row but every row ends at A (EDCBA, DCBA, CBA). Program 8 keeps a fixed top letter E on the left and shifts the stop upward (EDCBA, EDCB, EDC, ED, E).
Program 6 logs ascending letters ending at a fixed top letter (ABCDE, BCDE, CDE). This pattern logs descending letters starting at a fixed top letter — the mirror idea with reverse loops.
O(n²) where n is the number of rows. Total logged characters equal n+(n-1)+...+1 = n(n+1)/2 — same triangular count as Programs 1, 4, 5, and 7.
Yes. Keep letters = "ABCDEFG..." and console.log(letters.slice(i, rows).split("").reverse().join("")) for each row index i. Nested charCode loops teach the bounds; slice plus reverse is a compact shortcut.
Use parseInt with Number.isFinite after prompt(), then clamp rows between 1 and 26 so letter codes stay within A–Z.

Did you Know? 🔊

Each row starts at a fixed top letter and logs descending to a row-specific stop: top = "A".charCodeAt(0) + rows - 1, row i uses stop = "A".charCodeAt(0) + i and for (let code = top; code >= stop; code--). Compare Program 7 (every row ends at A) and Program 6 (ascending rows ending at fixed top letter).

Continue to Program 9

Alphabet pattern A, BB, CCC, ... — the next alphabet pattern in the series.

Program 9 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