JavaScript Reverse Alphabet Pyramid Pattern (Slice from E)

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

What Is This Pattern?

A right-aligned reverse alphabet pyramid prints a reverse slice each row (E, then E D, then E D C…) with empty width-2 cells on the left so columns line up.

Remember
Rule: pad left; each row prints top down to current letter

         E
       E D
     E D C
   E D C B
 E D C B A     ← top = E

Companion to Program 22: same fixed-width grid (" " pads + padStart(2, " ") letters), but each row is a reverse slice from the top letter — not a running counter across rows.

How to Solve It

Walk the row letter from top down to A. Pad first, then append letters from top down to that row letter.

MethodIdeaBest for
Letter-code loopsOuter i = row letter; pad A..i-1; letters top..iLearning the classic char-code style
Int row indexPad n - row; then row letters via top - kThinking in row numbers

Pseudocode

Pseudocode
base = code of 'A'
top  = code of 'E'   // or chosen top
for i from top down to base:     // row letter
    line = ""
    for j from base to i - 1:    // pad cells
        append "  "
    for j from top down to i:    // reverse slice
        append fromCharCode(j).padStart(2, " ")
    print line

Cheat sheet

GoalPattern
Boundsconst base = "A".charCodeAt(0); const top = "E".charCodeAt(0);
Walk each row letterfor (let i = top; i >= base; i--)
Pad cellsfor (let j = base; j < i; j++) line += " ";
Reverse slicefor (let j = top; j >= i; j--) line += String.fromCharCode(j).padStart(2, " ");
Pad counti - base (or n - row in int style)
End the rowconsole.log(line);
vs Program 22No running counter — each row restarts from top

Printing Letters vs Starting a New Line

APIEffectUse for
line += " " / padStartStays on the same rowEach empty cell and each letter cell
console.log(line)Ends the current rowAfter pad + letter loops

Append without a newline, then end the row once.

Live Preview

Change the size (top letter) and the reverse pyramid updates instantly — including pad count on the first row.

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

Live result 5 letters · top E · width 5
         E
       E D
     E D C
   E D C B
 E D C B A

Worked Walkthrough — top = D

Width is 4 cells. Trace pads and the reverse slice for each row letter.

Row letterPadsSlicePrinted row
D3DD
C2D CD C
B1D C BD C B
A0D C B AD C B A

Each of n rows does O(n) pad + letter work → O(n²) total.

JavaScript Programs

Three complete programs: fixed A–E, top-letter prompt, and an int row-index style. Use View Output for sample results, or Try It Yourself to edit and run in the playground.

Example 1 — Fixed A–E

First append padding pairs, then append letters from E down to the current row letter.

JavaScript
const base = "A".charCodeAt(0);
const top = "E".charCodeAt(0);

for (let i = top; i >= base; i--) {
  let line = "";
  for (let j = base; j < i; j++) {
    line += "  ";
  }
  for (let j = top; j >= i; j--) {
    line += String.fromCharCode(j).padStart(2, " ");
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Outer loop picks the row letter. i runs from E down to A.

2. Pad first. For each j from A up to (but not including) i, append " ".

3. Reverse slice. Append letters from E down to i with padStart(2, " ").

When i is C: two pad cells, then E D C. Pads shrink until the bottom row is E D C B A.

Example 2 — Top Letter Input

The pattern prints rows from the chosen top letter down to A. Validate a single A–Z character.

JavaScript
const raw = (prompt("Enter the top letter (like E):") || "").trim().toUpperCase();

if (!/^[A-Z]$/.test(raw)) {
  console.log("Please enter a single letter A-Z.");
} else {
  const top = raw.charCodeAt(0);
  const base = "A".charCodeAt(0);

  for (let i = top; i >= base; i--) {
    let line = "";
    for (let j = base; j < i; j++) {
      line += "  ";
    }
    for (let j = top; j >= i; j--) {
      line += String.fromCharCode(j).padStart(2, " ");
    }
    console.log(line);
  }
}
Try It Yourself

How It Works

1. Prompt and validate. Trim, uppercase, and require a single A–Z letter.

2. Same pad/letter core. Both the outer start and the letter-loop start use top — only the shared bound changes.

Example 3 — Int Row Index

Often clearer if you think in row numbers: pad n - row cells, then append row letters from the top down.

JavaScript
const top = "E".charCodeAt(0);
const base = "A".charCodeAt(0);
const n = top - base + 1;

for (let row = 1; row <= n; row++) {
  let line = "";
  for (let s = 0; s < n - row; s++) {
    line += "  ";
  }
  for (let k = 0; k < row; k++) {
    line += String.fromCharCode(top - k).padStart(2, " ");
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Pad by row number. Pad count is n - row.

2. Letters from the top. Index k uses String.fromCharCode(top - k) so row 1 is E, row 3 is E D C, and so on.

Same visual pyramid as the letter-code scan — only the indexing style changes.

Edge Cases & Pitfalls

Check these before calling the solution done.

width mismatch

One space vs two

Empty cells must be " " to match padStart(2, " "). A single space breaks column alignment.

vs Program 22

Counter vs slice

Program 22 keeps one running counter (A…O). This pattern restarts from top every row.

vs Program 20

Spaced vs tight

Program 20 prints tight reverse suffixes (EDCBA). This page uses width-2 cells (E D C B A).

log early

Broken columns

Call console.log only after both pad and letter loops finish.

top = A

Single A

Output is just a width-2 A cell (no pads) — a good sanity check.

Bad prompt

Validate one letter

Reject empty strings and multi-character input before computing top.

Time and Space Complexity

ProgramTimeExtra space
Letter-code loops (Examples 1–2)O(n²)O(n) for the current line string
Int row index (Example 3)O(n²)O(n) for the current line string

Each of n rows does O(n) pad + letter work, so total time is quadratic in the number of letters.

Key Takeaways

  • Rule: pad left; each row prints top down to the current letter.
  • Width: empty " " must match padStart(2, " ").
  • No counter: unlike Program 22, every row restarts from top.
  • Complexity: O(n²) from pad + letter work per row.

One line: for each row letter i, pad i - A cells, then print top down to i.

Frequently Asked Questions

The first appends padding so the visible letters are right-aligned. The second appends letters from E (or your top letter) down to the current row letter i.
Each letter is printed in a width-2 field, so two spaces keep empty cells the same width and the pyramid aligned in monospace output.
Program 22 uses one running counter across all rows (A..O). Program 23 prints a reverse slice each row using the loop variable j (E..i).
line += ' ' or line += ch.padStart(2, ' ') stays on the same row for pads and letters. console.log(line) ends the row after both inner loops finish.
Yes. Change the second loop to always begin from your chosen top letter and decrement to the row letter — Example 2 does this with user input.
O(n²) for n rows because each row does O(n) work across the padding + letter loops.
Use prompt().trim().toUpperCase(), require a single A–Z character, and reject empty or multi-character input.
For top = E, when i is E you print 4 pad cells (A..D); when i is A you print 0. Pads = (i - A).

Did you know?

Outer i runs from E down to A. The first inner loop prints one " " per j with A <= j < i (so the block shifts left each row). The second loop prints letters from E down to i using String.fromCharCode(j).padStart(2, " ") so each cell is 2 columns wide.

Next: Palindromic Alphabet Pyramid

Descending then ascending around a center A — A, BAB, CBABC, … EDCBABCDE.

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