JavaScript Repeating Alphabet Pattern (Inverted Forward)

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

What Is This Pattern?

An inverted forward repeating alphabet triangle prints one letter per row with a shrinking width, while letters advance from A toward the top letter.

Remember
Rule: widths shrink n…1; letter steps A → top

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

Same shrinking widths as Program 11, but letters run A → B → C → D → E instead of counting down. Outer loop picks the letter; inner loop appends it top - code + 1 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 A…top; inner repeats that letter fewer times each rowLearning, 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 base to top:
    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 forwardfor (let code = base; code <= top; code++)
Shrink 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));
Countdown twinLetters E…A with same widths → 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 inverted forward triangle updates instantly — including the last 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 · last E · 15 letters
AAAAA
BBBB
CCC
DD
E

Worked Walkthrough — rows = 4

With rows = 4, top letter is D. Trace each outer-loop code and the shrinking repeat count.

codeLetterrepeatPrinted row
AA4AAAA
BB3BBB
CC2CC
DD1D

Total letter characters: 4 + 3 + 2 + 1 = 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; first row is five As.

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

for (let code = base; code <= top; 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 advances letters. code runs from base up to top. Each value is the letter for that row.

3. Inner loop shrinks the width. repeat = top - code + 1 gives widths n, n-1, …, 1. 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 AAAAA; next is BBBB; last is a single E.

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 = base; code <= top; 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 letters stay 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 = base; code <= top; 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 advance. Still walk code from base up to top.

2. Build the row. ch.repeat(repeat) returns the letter repeated repeat times (n down to 1).

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.

wrong direction

Countdown instead of forward

Walk code from base to top. Counting down from top is Program 11.

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.

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 = n + (n-1) + … + 1 = n(n+1)/2, which is still quadratic in n.

Key Takeaways

  • Rule: widths shrink n…1; letters advance from A to the top letter.
  • Two loops: outer = letter A…top; 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 A to top, append it top - code + 1 times, then console.log.

Frequently Asked Questions

Program 11 prints EEEEE, DDDD, … (letters step down). Program 12 prints AAAAA, BBBB, … (letters step up) with the same inverted widths 5…1.
When code is A, repeat = top − A + 1 = 5, so five A’s. Next code is B, repeat = 4, so four B’s.
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.
repeat = top − code + 1. As code rises toward top, fewer iterations run, so fewer characters are appended.
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 n+(n−1)+…+1 = 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 letters stay within A–Z.

Did you know?

This is the forward-letter twin of Program 11: same inverted widths (5…1), but letters advance A→E instead of stepping down. Append the outer loop letter inside the inner loop so each row stays uniform.

Next: Sequential Alphabet Triangle

One running letter that keeps advancing across rows — A, then B C, then D E F…

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