JavaScript Reverse Alphabet Pattern (Diagonal Star)

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

What Is This Pattern?

A reverse alphabet diagonal-star pattern prints the reverse run top…A on every row, but replaces exactly one character with * where i === j. The star slides right → left.

Remember
Rule: each row is top…A; print * when i === j

EDCB*
EDC*A
ED*BA
E*CBA
*DCBA     ← top = E

Outer loop walks row keys A…top; inner loop walks columns top…A. One tiny condition turns a flat reverse grid into a moving diagonal.

How to Solve It

Build an n×n character grid. For each cell, print * or the column letter based on i === j.

MethodIdeaBest for
Reverse + diagonalInner j from top…A; star when i === jThis pattern (star slides right → left)
Forward + diagonalInner j from A…top; same i === j testStar slides left → right

Pseudocode

Pseudocode
base = code of 'A'
top  = code of peak letter
for i from base to top:
    line = ""
    for j from top down to base:
        if i == j:
            append '*'
        else:
            append fromCharCode(j)
    print line

Cheat sheet

GoalPattern
Row keyfor (let i = base; i <= top; i++)
Reverse columnsfor (let j = top; j >= base; j--)
Diagonal starline += (i === j) ? "*" : String.fromCharCode(j);
End the rowconsole.log(line);
Top from rowsconst top = "A".charCodeAt(0) + rows - 1;
Forward twinInner loop A…top → star slides left to right

Printing Letters vs Starting a New Line

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

Append without a newline, then end the row once.

Live Preview

Change the size (top letter) and the diagonal-star grid updates instantly.

Whole numbers from 1 to 10. Size n means top letter = A + n - 1. Tap a chip or type a value — the preview redraws as you go.

Live result 5×5 · top E
EDCB*
EDC*A
ED*BA
E*CBA
*DCBA

Worked Walkthrough — top = D (4×4)

Trace each row key i and where i === j places the star.

iStar at columnPrinted row
Arightmost (j = A)DCB*
Bone leftDC*A
Cone more leftD*BA
Dleftmost (j = D)*CBA

Each of n rows prints n characters → n² total work, so time is O(n²).

JavaScript Programs

Three complete programs: fixed top E, prompt for top letter, and a forward diagonal variant. Use View Output for sample results, or Try It Yourself to edit and run in the playground.

Example 1 — Fixed Top E

Outer i runs A..E. Inner j runs E..A. When i === j, append *; otherwise append the column letter.

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

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

How It Works

1. Set bounds. base is A; top is E (5×5 grid).

2. Outer loop picks the row key. i walks A..E — the letter that will become * on that row.

3. Inner loop prints reverse columns. j walks E..A. When i === j, append *; else append fromCharCode(j).

4. Break the line. console.log(line) after the inner loop finishes.

Row i = A matches the rightmost column → EDCB*. By i = E, the star is leftmost → *DCBA.

Example 2 — Top Letter Input

Read the top letter at runtime. Validate a single A–Z character from prompt().trim().toUpperCase().

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

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

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

How It Works

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

2. Same diagonal core. Only the bound top changes — with D you get a 4×4 grid and the star still slides right → left.

Example 3 — Forward A..E with Diagonal *

Flip the inner loop to A..E. The star now slides left → right on the main diagonal.

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

for (let i = base; i <= top; i++) {
  let line = "";
  for (let j = base; j <= top; j++) {
    line += (i === j) ? "*" : String.fromCharCode(j);
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Same diagonal rule. Still print * when i === j.

2. Flip column order. Inner loop walks A..E instead of E..A — letters go forward and the star travels left → right.

Comparing Examples 1 and 3 shows how inner-loop direction controls both letter order and which way the star moves.

Edge Cases & Pitfalls

Check these before calling the solution done.

wrong compare

No star / wrong diagonal

Use letter codes for both i and j. Comparing a row index (1..n) to a char code never matches cleanly.

log inside

Column of characters

Call console.log only after the inner loop. Logging inside splits one row across many lines.

Reuse line

Growing leftovers

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

top < A

Empty grid

If top < base, the outer loop never runs. Require a letter from A–Z.

top = A

Single *

Output is just * on one line — a good sanity check (1×1 grid).

Bad prompt

Validate one letter

Reject empty strings and multi-character input before using charCodeAt(0).

Time and Space Complexity

ProgramTimeExtra space
Reverse diagonal (Examples 1–2)O(n²)O(n) for the current line string
Forward diagonal (Example 3)O(n²)O(n) for the current line string

An n×n grid prints n characters per row across n rows — still quadratic in n.

Key Takeaways

  • Rule: each row is top…A; print * when i === j.
  • Diagonal: one star per row; reverse columns make it slide right → left.
  • Break the row: call console.log(line) only after the inner loop.
  • Complexity: O(n²) time for an n×n character grid.

One line: for each row key i, scan columns top…A and print * when i === j.

Frequently Asked Questions

i walks A…E down the rows while j scans E…A across columns (as letter codes). The condition i === j marks one diagonal position per row, which gets replaced by '*'.
Because i increases each row, but the printed letters go from E down to A across the row. The match i === j occurs at a different column each time, sliding the star left.
The star prints when i === j, so each row replaces exactly one character at the matching column.
Yes. Replace '*' with any symbol (like '#' or '@') in the conditional branch.
line += '*' or line += String.fromCharCode(j) stays on the same row. console.log(line) ends the row after the inner loop finishes.
You print forward letters instead of E…A, and the star slides the other way (left to right on the main diagonal).
O(n²) for an n×n letter grid because every row prints n characters.
Use prompt().trim().toUpperCase(), require a single A–Z character, and reject empty input. Cap at Z if you only want alphabetic ranges.

Did you know?

The diagonal is defined by i === j while the row prints letters from E down to A. Since i increases A..E each row, the star moves one position left each line: EDCB*, EDC*A, ED*BA, E*CBA, *DCBA.

Next: Palindromic Alphabet Pyramid

Mirror letters around a peak — A, ABA, ABCBA, ABCDCBA…

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