JavaScript Repeating Alphabet Triangle Pattern (Reverse)

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

What Is This Pattern?

A reverse repeating alphabet triangle prints one letter per row, repeated to match the row width, while letters count down from the top letter toward A.

Remember
Rule: row width grows 1, 2, 3…; letter steps top → A

E
DD
CCC
BBBB
AAAAA     ← 5 rows (top = E)

Same growing widths as Program 9, but letters run E → D → C → B → A. Solve it with nested loops: outer picks the letter (counting down), inner appends that letter the right number of times, then console.log(line) ends the row.

How to Solve It

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

MethodIdeaBest for
Nested loopsOuter = letter countdown; inner repeats that letterLearning, interviews, exams
ch.repeat(n)Build a full repeated-letter row in one callShorter demos once loops click

Pseudocode

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

Cheat sheet

GoalPattern
Top letter for n rowsconst top = "A".charCodeAt(0) + rows - 1;
Walk letters downfor (let code = top; code >= base; code--)
Repeat countconst repeat = top - code + 1;
Append outer letterline += String.fromCharCode(code); (not the inner index)
End the rowconsole.log(line);
One-line row shortcutconsole.log(ch.repeat(repeat));
Inverted twinShrink widths 5…1 → Program 11

Printing Letters vs Starting a New Line

APIEffectUse for
line += chStays on the same rowEach repeated 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 repeating triangle updates instantly — including the top letter and triangular total.

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

Live result 5 rows · top E · 15 letters
E
DD
CCC
BBBB
AAAAA

Worked Walkthrough — rows = 4

With rows = 4, top letter is D. Trace each outer-loop code and the repeated letter on that row.

codeLetterrepeatPrinted row
DD1D
CC2CC
BB3BBB
AA4AAAA

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 String.repeat 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. Top letter becomes E.

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

for (let code = top; code >= base; code--) {
  const ch = String.fromCharCode(code);
  const repeat = top - code + 1;
  let line = "";
  for (let k = 0; k < repeat; k++) {
    line += ch;
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Set height and bounds. rows = 5; base is A; top is E (base + rows - 1).

2. Outer loop counts letters down. code runs from top down to base. Each value is the letter for that row.

3. Inner loop repeats that letter. repeat = top - code + 1 gives widths 1, 2, 3, … Append ch (the outer letter), not the inner index.

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

First row is E; second is DD; last is AAAAA.

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);
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 code = top; code >= base; code--) {
    const ch = String.fromCharCode(code);
    const repeat = top - code + 1;
    let line = "";
    for (let k = 0; k < repeat; k++) {
      line += ch;
    }
    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 the top letter stays in A–Z.

3. Same nested-loop core. Only the source of rows (and thus top) changes — the print logic matches Example 1.

Example 3 — ch.repeat(repeat)

Build each repeated-letter row in one call — same shape, no explicit inner append loop.

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

for (let code = top; code >= base; code--) {
  const ch = String.fromCharCode(code);
  const repeat = top - code + 1;
  console.log(ch.repeat(repeat));
}
Try It Yourself

How It Works

1. Same outer countdown. Still walk code from top down to base.

2. Build the row. ch.repeat(repeat) returns the letter repeated repeat times.

3. Print and advance. console.log prints that string and ends the line.

Learn the two-loop version first (Examples 1–2) so you can explain both bounds; treat this as a polish shortcut afterward.

Edge Cases & Pitfalls

Check these before calling the solution done.

inner letter

Mixed letters on a row

Append the outer letter (ch), not the inner counter. Printing the inner index changes letters across the row.

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 inner loop.

Reuse line

Growing leftovers

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

rows = 1

Single A

Output is just A on one line — a good sanity check (top === base).

rows > 26

Beyond Z

Clamp or reject — top leaves A–Z. Keep rows in 1…26 for alphabet demos.

Bad prompt

Use Number.isFinite

Bare parseInt(prompt()) yields NaN on letters — 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
ch.repeat (Example 3)O(n²)O(n) per temporary row string

Total letters logged = 1 + 2 + … + n = n(n+1)/2, which is still quadratic in n.

Key Takeaways

  • Rule: widths grow 1, 2, 3…; letters step from the top letter down to A.
  • Two loops: outer = letter countdown; inner repeats that outer 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 letter from top down to A, append it repeat times, then console.log.

Frequently Asked Questions

Program 9 uses A, BB, CCC, … (letters increase). Program 10 uses E, DD, CCC, … (letters decrease) with the same growing repeat counts.
On the 4th row the outer-loop letter is B, and the inner loop runs four times, appending B each time.
The inner loop only controls how many times to append. Using the outer letter keeps the row uniform; using the inner counter would change letters across the row.
line += ch builds the full row string. console.log() inside the inner loop would log one letter per line. Log once after the inner loop finishes.
O(n²) where n is the number of rows. Total logged characters equal 1+2+…+n = n(n+1)/2.
Yes. console.log(ch.repeat(repeat)) logs a full repeated-letter row in one call. Nested loops are better for learning; ch.repeat is a handy shortcut later.
Use parseInt(prompt(...), 10), check Number.isFinite(rows), and clamp between 1 and 26 so the top letter stays within A–Z.
Top letter math can go past Z. Clamp to 26 for A–Z demos, or define a clear wrap/error policy.

Did you know?

This pattern is the reverse of Program 9: widths still grow 1, 2, 3, …, but letters run backward (E, then D, then C, …). Append the outer loop letter inside the inner loop so each row stays uniform.

Next: Inverted Repeating Triangle

Letters still step E→A, but widths shrink 5, 4, 3…1 instead of growing.

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