JavaScript Alphabet Triangle Pattern (Reverse Start)

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

What Is This Pattern?

A reverse-starting-letter alphabet triangle grows leftward: each row begins one letter earlier, but letters along the row still run forward to a fixed top.

Remember
Rule: start letter moves top→A; print forward to top

E
DE
CDE
BCDE
ABCDE     ← top = 'E' (5 rows)

Mixes Program 1 (forward run) with Program 2 (moving start). The right edge stays fixed at top on every row.

How to Solve It

Two ways to emit the same shape — nested charCodeAt loops, or a growing suffix of a fixed alphabet string.

MethodIdeaBest for
Nested code loopsOuter = start code; inner = start up to topLearning, interviews, exams
Alphabet suffixSlice the last r letters of "ABC…top"Shorter demos once loops click

Pseudocode

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

Cheat sheet

GoalPattern
Top from rowsconst top = "A".charCodeAt(0) + rows - 1;
Move the startfor (let i = top; i >= base; i--)
Print forwardfor (let j = i; j <= top; j++) line += String.fromCharCode(j);
End the rowconsole.log(line);
Fixed right edgeInner loop always stops at top
Descending along rowSee Program 2
Growing peak, print backSee Program 4

Printing Letters vs Starting a New Line

APIEffectUse for
line += chStays on the same rowEach letter (and optional space)
console.log(line)Ends the current rowAfter the inner loop finishes a row

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

Live Preview

Change the row count and the reverse-start forward triangle updates instantly — every row ends at the same top letter.

Whole number from 1 to 10. Tap a chip or type a count — the preview redraws as you go.

Live result 5 rows · top E · 15 letters
E
DE
CDE
BCDE
ABCDE

Worked Walkthrough — Top = E (5 rows)

Trace each start letter and the resulting forward suffix.

i (start)Inner rangePrinted row
EE..EE
DD..EDE
CC..ECDE
BB..EBCDE
AA..EABCDE

Row lengths are 1, 2, 3, 4, 5. The right edge is always E.

JavaScript Programs

Three complete programs: fixed top E, row-count prompt, and spaced letters. Use View Output for sample results, or Try It Yourself to edit and run in the playground.

Example 1 — Fixed Top E

Outer loop chooses the first letter on the row; inner loop appends forward up to E.

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

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

How It Works

1. Outer loop moves the start. i runs from the code of E down to A — one start letter per row.

2. Inner loop appends forward. For each row, j runs from i up to top. When i is C, that builds CDE.

3. Break the line. console.log(line) after the inner loop ends the row.

4. Full last row. When i is A, the inner loop builds the full forward run ABCDE.

Example 2 — Row Count Input

Read the number of rows and compute top = "A".charCodeAt(0) + rows - 1. Validate with parseInt and clamp to 26.

JavaScript
let rows = parseInt(prompt("Enter the number of rows:"), 10);
const base = "A".charCodeAt(0);

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

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

How It Works

1. Scale the top. For 4 rows, top becomes the code of D. Cap rows at 26 so top stays within A–Z.

2. Same core loops. Only the stop letter changes; the start-moves-backward rule is unchanged.

3. Safer input tip. Always check Number.isFinite(rows) before the loops — bare parseInt(prompt()) yields NaN on letters.

Example 3 — Spaced Letters

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

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

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

How It Works

1. Same bounds. Loop limits are unchanged — only the appended unit becomes fromCharCode(j) + " ".

2. Optional trim. Use line.trimEnd() later if you need a compact line for grading scripts.

Edge Cases & Pitfalls

Check these before calling the solution done.

Inner j--

Wrong direction on the row

If the inner loop counts down, you get Program 2 (E, ED, EDC). Keep j++ from i to top.

Outer ++

Growing from A instead

If the outer loop runs A..top with the same inner rule, you print Program 1 upside-down. Start at top and go down to A.

log inside

Column of letters

If console.log is inside the inner loop, each letter lands on its own line. Append with +=; call console.log only after the row.

rows = 1

Single A

When top is A, you print one row: A. A good sanity check.

rows > 26

Beyond Z

Cap at 26 (or reject) so top stays within A–Z.

Bad prompt

Use Number.isFinite

Bare parseInt(prompt()) yields NaN on empty or non-numeric text. Validate before the outer loop.

Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–2)O(n²)O(n) for the current line string
Spaced letters (Example 3)O(n²)O(n) for the current line string

For n rows, total letters are 1 + 2 + … + n = n(n + 1)/2 — still quadratic in n.

Key Takeaways

  • Rule: start letter moves top → A; print forward to top.
  • Fixed right edge: every row ends at the same top letter.
  • Break the row: call console.log(line) only after the inner loop.
  • Complexity: O(n²) time from the triangular letter count.

One line: for each start from top down to A, append that letter forward up to top, then console.log.

Frequently Asked Questions

The outer loop moves the starting letter from E down to A. The inner loop always appends forward from that start letter up to E (or top), so the last character remains E on every row.
Program 2 prints letters descending along the row (E, ED, EDC). This program prints forward along the row (E, DE, CDE) while only the row’s first letter moves backward.
Program 1 always starts at A and grows the end letter (A, AB, ABC). This pattern grows the start letter backward while keeping the right edge fixed at top.
Yes. Read rows with prompt(), set top = "A".charCodeAt(0) + rows - 1, then loop code from top down to A and append j from code up to top.
O(n²) for n rows, because the total printed letters are 1+2+...+n = n(n+1)/2.
Use parseInt(prompt(...), 10), check Number.isFinite(rows), require n ≥ 1, and cap at 26 so the top letter stays within A–Z.
Because the inner loop always stops at top (E in the fixed example), so the last printed character is always that same letter.
Yes. Use "a".charCodeAt(0) as the base: top = "a".charCodeAt(0) + rows - 1, then loop the same way with j++ up to top.

Did you know?

This triangle changes only the starting letter of each row (E, D, C, …), while letters along the row still increase forward. In the 5-row example, every row ends at E, producing E, DE, CDE, BCDE, ABCDE.

Next: Reverse Order Alphabet Triangle

Flip the inner direction: each row’s peak grows forward, but letters print backward down to A.

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