JavaScript Inverted Alphabet Triangle Pattern

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

What Is This Pattern?

An inverted alphabet right-angled triangle prints the longest letter run first, then shrinks by one letter each row — still starting from A every time.

Remember
Rule: for end letter i from last down to A,
      print A through i

ABCDE
ABCD
ABC
AB
A         ← 5 rows (longest on top)

It is the flip of Program 1: keep the same inner A..i print, reverse only the outer loop. Compare with Program 6, which shrinks from the left instead.

How to Solve It

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

MethodIdeaBest for
Nested charCode loopsOuter = shrinking end; inner = A..endLearning, interviews, exams
letters.slice(0, i)Slice shorter prefixes while i counts downShorter demos once loops click

Pseudocode

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

Cheat sheet

GoalPattern
Countdown lengthfor (let i = rows; i >= 1; i--)
Start codeconst start = "A".charCodeAt(0);
Print A..endfor (let code = start; code < start + i; code++) line += String.fromCharCode(code);
End the rowconsole.log(line);
One-line row shortcutconsole.log(letters.slice(0, i)); while i shrinks
Growing twinfor (let i = 1; i <= rows; i++) → Program 1

Printing Letters vs Starting a New Line

APIEffectUse for
line += chStays on the same lineEach letter
console.log(line)Ends the current lineAfter the inner loop

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

Live Preview

Change the row count and the inverted triangle updates instantly — capped at 26 letters (A–Z).

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

Live result 5 rows · 15 letters
ABCDE
ABCD
ABC
AB
A

Worked Walkthrough — rows = 4

Trace each outer-loop length as i counts down from 4 to 1.

Length iCodesPrinted rowLetters
4A..DABCD4
3A..CABC3
2A..BAB2
1A..AA1

Total letter prints: 4 + 3 + 2 + 1 = 10 = 4×5/2 — same as Program 1, only the print order of rows differs.

JavaScript Programs

Three complete programs: fixed rows, prompt input, and a slice 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 — outer loop shrinks; inner loop still prints A through the current length.

JavaScript
let rows = 5;
const start = "A".charCodeAt(0);

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

How It Works

1. Outer loop picks the length. i runs from 5 down to 1 — longest row first.

2. Inner loop restarts at A. For each i, code runs from start through start + i - 1, so the row is A through the i-th letter.

3. Print letters, then break the line. line += stays on the row; console.log(line) after the inner loop starts the next (shorter) row.

When i = 5 you get ABCDE; when i = 1 you get A.

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 start = "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;

  for (let i = rows; i >= 1; i--) {
    let line = "";
    for (let code = start; code < start + i; code++) {
      line += String.fromCharCode(code);
    }
    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 codes stay in A–Z.

3. Same shrink core. Only the source of rows changes — the print logic matches Example 1.

Example 3 — letters.slice(0, i)

Build each shrinking prefix in one call — same shape, no explicit inner letter loop.

JavaScript
let rows = 5;
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

for (let i = rows; i >= 1; i--) {
  console.log(letters.slice(0, i));
}
Try It Yourself

How It Works

1. Count length down. i runs from rows down to 1.

2. Slice the prefix. letters.slice(0, i) is ABCDE, then ABCD, and so on.

3. Learn loops first. Use Examples 1–2 when you need to show nested bounds; treat this as a polish shortcut afterward.

Edge Cases & Pitfalls

Check these before calling the solution done.

i++

Growing triangle by mistake

If you increment i from 1 to rows, you reprint Program 1. Use i-- from rows down to 1.

Stop at 2

Missing final A

Stopping before i = 1 drops the tip row. The condition must be i >= 1.

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.

rows > 26

Past Z

Codes leave A–Z when rows > 26. Clamp or reject in interactive programs.

rows = 1

Single A

Output is just A — longest and shortest coincide. A good sanity check.

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(rows²)O(n) for the current line string
letters.slice(0, i) (Example 3)O(rows²)O(n) per temporary row string

Total letters = n + (n - 1) + … + 1 = n(n + 1)/2 — still quadratic in n. Same totals as Program 1.

Key Takeaways

  • Rule: countdown length i; print A through the i-th letter each row.
  • Flip of Program 1: same inner loop — only reverse the outer loop.
  • Break the row: call console.log only after the inner loop.
  • Complexity: O(n²) time from the triangular letter count.

One line: for length i from rows down to 1, append A through the i-th letter, then console.log.

Frequently Asked Questions

The outer loop counts down so the first row prints the most letters and each next row prints one fewer. The inner loop still prints from A up to the current bound.
Because the inner loop always starts at A. That resets the sequence each row, producing ABCDE then ABCD and so on.
Program 1 grows row length (A, AB, ABC). This pattern shrinks it (ABCDE, ABCD, ABC). Only the outer loop direction changes; the inner loop is the same A..i print.
Program 4 grows while printing backward (A, BA, CBA). This pattern shrinks while printing forward from A (ABCDE, ABCD, ABC).
line += stays on the same line. console.log ends the current line. Letters use +=; the row break uses console.log after the inner loop.
O(n²) for n rows, because total printed characters are n(n+1)/2.
Yes. Keep a string of A–Z and console.log(letters.slice(0, i)) while i counts down from rows to 1. Nested charCode loops are better for learning; slice is a handy shortcut later.
Use parseInt(prompt(...), 10), check Number.isFinite(rows), and clamp between 1 and 26 so letter codes stay within A–Z.

Did you know?

This is the inverted twin of Program 1: same inner A..i print, only the outer loop shrinks. For 5 rows: ABCDE, ABCD, ABC, AB, A. Total letters still equal n(n+1)/2.

Next: Increasing Start Letter

Keep a fixed end letter and move the start letter right each row.

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