JavaScript Sequential Alphabet Triangle Pattern (Decreasing)

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

What Is This Pattern?

A continuous decreasing alphabet triangle prints letters in one unbroken sequence while row length shrinks: longest row first (A B C D E), then F G H I, … down to a single letter.

Remember
Rule: one running counter; row lengths n, n-1, …, 1

A B C D E
F G H I
J K L
M N
O             ← 5 rows (letters A–O)

Contrast Program 13 (same counter, growing rows) and Program 5 (decreasing rows but letters reset to A each line).

How to Solve It

Keep one counter outside the outer loop. Walk row length from n down to 1, appending that many letters each time.

MethodIdeaBest for
String appendline += ch + " " then console.log(line)Learning the classic style
row.join(" ")Push letters into an array, join onceNo trailing space

Pseudocode

Pseudocode
rows = n (e.g. 5)
code = code of 'A'
for rowLen from rows down to 1:
    line = ""
    repeat rowLen times:
        append fromCharCode(code) (+ space)
        code = code + 1
    print line

Cheat sheet

GoalPattern
Start counterlet code = "A".charCodeAt(0); — outside the outer loop
Shrink row lengthfor (let rowLen = rows; rowLen >= 1; rowLen--)
Append lettersfor (let i = 0; i < rowLen; i++) { line += String.fromCharCode(code) + " "; code++; }
Clean joinrow.push(ch); then console.log(row.join(" "));
Letter budgetn(n+1)/2 <= 26 → cap n at 6 for A–Z only
End the rowconsole.log(line);

Printing Letters vs Starting a New Line

APIEffectUse for
line += ch + " "Stays on the same rowEach letter (and its separator)
console.log(line)Ends the current rowAfter the inner loop

Append without a newline, then end the row once.

Live Preview

Change the row count and the decreasing continuous triangle updates instantly — including letter total and last letter.

Whole numbers from 1 to 6 (keeps letters in A–Z). Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 15 letters · last O
A B C D E
F G H I
J K L
M N
O

Worked Walkthrough — rows = 4

Trace each row length and the continuous letter range. Total = 4×5/2 = 10 (A–J).

rowLenLettersPrinted row
4A..DA B C D
3E..GE F G
2H..IH I
1JJ

Total letters n(n+1)/2 → O(n²) work.

JavaScript Programs

Three complete programs: fixed 5 rows, row-count prompt, and a join style with no trailing space. 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.

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

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

How It Works

1. Keep one counter. code starts at A and lives outside the outer loop so letters never restart.

2. Shrink row length. rowLen walks from 5 down to 1 — longest row first.

3. Append and bump. Each inner iteration appends the current letter and does code++.

First row takes A–E; the next continues at F — continuous across the whole triangle.

Example 2 — User Input Version

Read rows and clamp to 1–6 for A–Z demos (6×7/2 = 21 letters).

JavaScript
let rows = parseInt(prompt("Enter number of rows (max 6):"), 10);

if (!Number.isFinite(rows) || rows < 1) {
  console.log("Please enter a whole number >= 1.");
} else {
  if (rows > 6) rows = 6;
  let code = "A".charCodeAt(0);

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

How It Works

1. Prompt and validate. Parse with parseInt, reject invalid values, clamp to 6 for A–Z.

2. Same running-counter core. Only the outer bound changes — the code++ logic matches Example 1.

Example 3 — row.join(" ") Variant

Collect letters in an array, then join with spaces for tidy rows with no trailing space.

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

for (let rowLen = rows; rowLen >= 1; rowLen--) {
  const row = [];
  for (let i = 0; i < rowLen; i++) {
    row.push(String.fromCharCode(code));
    code++;
  }
  console.log(row.join(" "));
}
Try It Yourself

How It Works

1. Same code++ logic. Only formatting changes — letters still continue across rows.

2. Join once. row.join(" ") inserts spaces between letters without a trailing space at the end of the line.

Edge Cases & Pitfalls

Check these before calling the solution done.

reset counter

Restarting at A each row

If you reset code inside the outer loop, you get Program 5 (ABCDE, ABCD, …). Keep the counter outside.

vs Program 13

Growing vs shrinking

Program 13 uses 1..n (growing). This pattern uses n..1 (shrinking) with the same continuous counter.

past Z

Letter budget

Total letters = n(n+1)/2. Cap n at 6 for A–Z only, or define wrap/stop past Z.

wrong direction

1..n instead of n..1

Looping rowLen upward prints Program 13’s growing shape, not this decreasing triangle.

n = 1

Single A

Output is just A — a good sanity check.

log early

One letter per line

Call console.log only after the inner loop finishes building the full row.

Time and Space Complexity

ProgramTimeExtra space
String append (Examples 1–2)O(n²)O(n) for the current line string
join style (Example 3)O(n²)O(n) for the current row array

Total letters are n(n+1)/2, so work is quadratic in the number of rows either way.

Key Takeaways

  • Rule: one running counter; row lengths n, n-1, …, 1.
  • Never reset: keep code outside the outer loop.
  • Budget: letters = n(n+1)/2 — cap at 6 rows for A–Z.
  • Complexity: O(n²) from the triangular letter count.

One line: for each length from n down to 1, print that many letters from a shared counter.

Frequently Asked Questions

Because code is initialized once before the outer loop and incremented after every printed character. The counter never resets, so letters stay consecutive across the whole triangle.
Program 13 grows row width (1, 2, 3, … letters). This pattern shrinks width (n, n-1, …, 1) while using the same running counter — A B C D E then F G H I, not A then B C.
Program 5 also uses decreasing row lengths but resets to A each row (ABCDE, ABCD, ABC, …). Here letters continue: A B C D E, F G H I, J K L, M N, O.
That yields rows, rows-1, …, 1 — exactly how many letters each row needs, longest first. Program 13 uses 1..rows for the opposite (growing) shape.
line += String.fromCharCode(code) + ' ' stays on the same row for each letter. console.log(line) ends the current row after the inner loop finishes.
1+2+…+n = n(n+1)/2. For 5 rows that is 15 letters (A through O).
O(n²) where n is the number of rows. Total printed letters equal n(n+1)/2.
Use parseInt(prompt(...), 10), check Number.isFinite(rows), then clamp between 1 and 6 so n(n+1)/2 stays within A–Z for demos.

Did you know?

One running counter prints letters continuously while row length shrinks: 5 letters, then 4, 3, 2, 1. Total letters for n rows is still n(n+1)/2 — compare Program 13 (growing rows) and Program 5 (decreasing rows but letters reset each line).

Next: Rotating Alphabet Pattern

Circular shifts of A–E — ABCDE, BCDEA, CDEAB, DEABC, EABCD.

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