Decreasing Alphabet Pattern in JavaScript

Beginner
⏱️ 8 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Nested Loops

What You’ll Learn

The decreasing alphabet pattern is the mirror of Program 1’s growing triangle: the first row is longest, each line drops one letter at the end. This tutorial covers the shape rule, reverse outer loop, a live preview, algorithm steps, worked JavaScript examples, edge cases, and complexity.

Shape Rule

A..end letter, shrinking rows

Row 1 prints ABCDE (all rows letters), then ABCD, …, down to a single A.

Outer Loop

Rows

for (let i = rows; i >= 1; i--) picks how many letters each row prints - longest first.

Inner Loop

Letters

for (let code = base; code < base + i; code++) still prints letters from A; only the outer bound i shrinks each row.

line vs console.log

Same line / next line

Letters use line += String.fromCharCode(code); end each row with console.log(line).

Live Preview

1–26 rows

Pick a row count and draw the decreasing alphabet pattern instantly in the browser.

O(n²)

Complexity

Total letters = n(n+1)/2; extra memory stays O(1).

Introduction

A decreasing alphabet pattern starts with the longest row and shortens by one letter each line. With five rows the console shows ABCDE, ABCD, ABC, AB, A - the inverse of Program 1’s growing triangle.

in JavaScript you solve it with two nested for loops: the outer loop walks i from rows down to 1, the inner loop prints letters from A through i characters, then console.log(line) moves to the next line.

Why it matters?

It reinforces reverse outer-loop bounds - the same inner letter logic as Program 1, flipped. Once for (let i = rows; i >= 1; i--) clicks, inverted stars, numbers, and more patterns follow naturally.

Key Highlights

Row = Shrinking Length

On row i, print i letters from A; first row has rows letters.

Reverse Outer Loop

for (let i = rows; i >= 1; i--) walks longest row first.

print Then Break

line += String.fromCharCode(code) in the inner loop; console.log(line) after.

Program 1 Mirror

Same inner loop; only outer direction differs from the increasing triangle.

In short: for each row i from rows down to 1, print letters from A with line += String.fromCharCode(code), then call console.log(line).

📝 Problem & Approach

Given a positive integer rows, print a left-aligned decreasing alphabet pattern: the first line has rows letters from A, each next line one fewer, ending with A.

JavaScript
// First 5 rows (conceptual shape)
// ABCDE
// ABCD
// ABC
// AB
// A

Inputs & Outputs

ItemTypeDescription
rowsintNumber of triangle lines to print (typically ≥ 1).
Printed outputtextLeft-aligned rows of letters; first row has rows letters from A, each row one shorter.

Minimal workflow

Pseudocode
for i from rows down to 1:
    for j from 1 to i:
        print next letter from A (no newline)
    print newline

Approach comparison

ApproachIdeaBest for
Nested loopsDecreasing outer + inner letters from ALearning and interviews
Reverse outer loopfor (let i = rows; i >= 1; i--)Decreasing row lengths - this pattern
letters.slice(0, i)Slice first i letters with decreasing iShorter production-style demos

⚡ Quick Reference

GoalPattern
Walk each row (decreasing)for (let i = rows; i >= 1; i--)
Print A..end lettersfor (let code = base; code < base + i; code++) { line += String.fromCharCode(code); }
End the rowconsole.log(line)
One-line row shortcutconsole.log(letters.slice(0, i)) inside decreasing outer loop
Growing variantUse for (let i = 1; i <= rows; i++) - see Program 1

📋 line vs console.log vs slice

Same decreasing pattern - different ways to emit characters.

line += String.fromCharCode(code)
same line

Prints a letter without moving to the next line

console.log(line)
new line

Ends the current row after all letters are printed

letters.slice(0, i)
whole row

Builds letters A..end at once - skip the inner loop

Learning tip
loops first

Master nested loops before the string shortcut

Context

When This Pattern Shows Up

Reach for this pattern when teaching reverse outer loops or mirroring Program 1’s growing triangle.

  1. Reverse-loop practice

    Natural follow-up after Program 1 - same inner logic, outer loop counts down.

  2. Nested-loop warm-up

    Practice for (let i = rows; i >= 1; i--) with an immediate visual check.

  3. prompt() input practice

    Combine loops with prompt() for a flexible row count.

  4. Gateway to variants

    Leads to left-trim patterns, pyramids, and hollow shapes in the series.

  5. Not a UI layout tool

    This is a console teaching pattern - not how you build modern app screens.

Key benefit: one small program that locks in reverse outer loops, output sequencing, and O(n²) thinking - the mirror image of Program 1.

🔮 Live Preview

Choose a row count between 1 and 20 and draw the decreasing alphabet pattern in the browser.

Try 5, 7, or 10. Cap is 26 letters (A-Z).

Live result
Press "Draw pattern".

Examples Gallery

Three complete JavaScript programs - fixed row count, prompt input, and a letters.slice(0, i) shortcut. Click View Output to reveal sample console results, or Try it Yourself to run in the browser editor.

📚 Getting Started

Print five rows with classic nested loops - longest row first.

Example 1 — Fixed rows = 5

Hard-coded height - ideal for first demos and screenshots.

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

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

How It Works

When i = 5, the inner loop builds ABCDE. When i = 4, it builds ABCD, and so on until i = 1 builds a single A. console.log(line) after the inner loop starts the next row.

📈 Practical Variant

Let the user choose the height at runtime.

Example 2 — User Input Version

Read the row count with prompt() and convert with parseInt() (validate with Number.isFinite in real apps).

JavaScript
const rowsInput = prompt("Enter the number of rows (max 26):");
let rows = parseInt(rowsInput, 10);
rows = Math.max(1, Math.min(rows, 26));
const base = "A".charCodeAt(0);

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

How It Works

Same charCodeAt/fromCharCode core as Example 1; only the source of rows changes. The outer loop still counts down from the clamped value. Non-numeric input yields NaN with bare parseInt(prompt()) - validate with Number.isFinite for safer labs.

⚡ Shortcut Style

Same shape without an explicit inner letter loop.

Example 3 — letters.slice(0, i)

Slice A-Z for each shrinking row length with letters.slice(0, i) inside a decreasing outer loop.

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

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

How It Works

letters.slice(0, i) returns the first i letters of the alphabet. With i counting down from rows, you get the same ABCDE-to-A shape without an explicit inner loop. Keep the two-loop version for exams that ask you to show both bounds.

🧠 How the Algorithm Prints Rows

1

Set up

Use prompt() when reading input. Set rows (fixed or from user) and clamp to 1-26 for A-Z.

Setup
2

Outer loop (rows, decreasing)

for (let i = rows; i >= 1; i--) selects how many letters the current line prints - longest first.

Row
3

Inner loop (letters)

for (let code = base; code < base + i; code++) prints each letter with line += String.fromCharCode(code).

Letters
4

New line

console.log(line) ends the row so the next outer iteration starts fresh.

Break
=

Decreasing alphabet pattern complete

Total letters: n+(n-1)+…+1 = n(n+1)/2O(n²) time, O(1) extra memory.

🔎 Worked Walkthrough — rows = 4

Trace each outer-loop value i (counting down) and see what the inner loop prints from A.

Outer iInner code rangePrinted rowLetters this row
4A..DABCD4
3A..CABC3
2A..BAB2
1A..AA1

Total letter prints: 4 + 3 + 2 + 1 = 10 = 4×5/2. Same triangular total as Program 1 - only row order differs.

Use Cases

Where this shrinking letter pattern (and its reverse outer loop) shows up beyond the homework prompt.

1. Teaching Reverse Loops

Clearest visual proof that for (let i = rows; i >= 1; i--) shrinks row length each iteration.

Example: compare side-by-side with Program 1.

2. Pattern Series Bridge

Natural step after Program 1 before left-trim and pyramid letter patterns.

Example: Program 6 shifts the start letter each row.

3. Console Formatting Drills

Practice character loops and line += String.fromCharCode(code)/console.log(line) with a shape that differs visibly from Program 1.

Example: swap outer loop direction and compare outputs.

4. Character Substitution

Swap to lowercase or digits once the letter loop works.

Example: print lowercase a..z once uppercase clicks.

5. Complexity Intuition

Triangular totals make O(n²) concrete for beginners.

Example: count printed letters for n = 10 → 55.

6. Input Validation Labs

Pair the pattern with Number.isFinite and positive-row checks.

Example: reject rows <= 0 and re-prompt.

Pro Tip: when an interviewer asks for the decreasing variant, explain that only the outer loop changes - inner letter logic matches Program 1.

Advantages

Why this decreasing pattern earns a spot after Program 1 in beginner JavaScript courses.

  1. 1. Instant Visual Contrast

    Side-by-side with Program 1 makes reverse outer loops obvious.

  2. 2. Minimal Concepts

    Only loops and console output - no arrays or math libraries.

  3. 3. Easy to Compare

    One-line outer-loop change flips between growing and shrinking shapes.

  4. 4. O(1) Extra Memory

    Streaming output needs no storage beyond loop counters.

Pro Tip: master Program 1 first, then this page - the inner loop is identical; only for (let i = rows; i >= 1; i--) is new.

Usage Tips

Small habits that keep decreasing alphabet-pattern code clean.

  1. 1. Name Bounds Clearly

    Use rows (or n) and keep i/j for row/column - or rename to row/col.

  2. 2. Validate parseInt(prompt()) with Number.isFinite

    Avoid crashes when the user types letters instead of a number.

  3. 3. Keep console.log(line) Outside

    Only call console.log(line) after the inner loop finishes the row.

  4. 4. Use Decreasing Outer Loop

    for (let i = rows; i >= 1; i--) matches “first row longest, each row one shorter” naturally.

  5. 5. Dry-Run One Small n

    Trace rows = 3 on paper - expect ABC, AB, A - before coding larger demos.

Pro Tip: if the output is a vertical list of single letters, you almost certainly put console.log(line) inside the inner loop.

Common Pitfalls

Mistakes that commonly break decreasing alphabet patterns.

  1. 1. console.log(line) Inside the Inner Loop

    Each letter lands on its own line - you get a column, not a shrinking row pattern.

    → Use line += String.fromCharCode(code) for letters; console.log(line) only after the inner loop.

  2. 2. Wrong Outer Loop Direction

    for (let i = 1; i <= rows; i++) prints Program 1’s growing triangle, not ABCDE-to-A.

    → For this shape, use for (let i = rows; i >= 1; i--).

  3. 3. Forgetting the Row Break

    Omitting console.log(line) after the inner loop glues every letter onto one endless line.

    → Always end the row after the inner loop.

  4. 4. Blind parseInt(prompt())

    Non-numeric input yields NaN with bare parseInt(prompt()).

    → Check with Number.isFinite and clamp the range.

  5. 5. Confusing with Program 1

    Copying Program 1’s outer loop produces A, AB, ABC - the opposite shape.

    → Decreasing pattern: outer counts down; inner still uses for (let code = base; code < base + i; code++).

Edge Cases

Check these inputs before calling the solution done.

rows = 1

Single letter last row

Output ends with just A on the last line.

rows = 0

Empty pattern

Outer loop never runs - print nothing or show a message.

Negative

rows < 0

Treat as invalid; re-prompt instead of silent empty output.

Large n

Many rows

Output grows as n²/2 characters - fine for labs, noisy for huge n.

Bad input

Non-numeric input

parseInt(prompt()) yields NaN - validate first.

Fill char

Not only uppercase

Same loops work with #, digits, or letters.

🎯 Practice Problems

Try these variations to lock in the decreasing pattern.

1. Flip to increasing

  • Outer loop from 1 to rows
  • Compare with Program 1

2. Print digits instead

  • Replace String.fromCharCode(code) with digit logic
  • Same shrinking outer loop structure

3. Safe input loop

  • Use Number.isFinite until rows >= 1
  • Then draw the decreasing pattern

4. Continue the series

  • Try Program 6 - left-trim each row
  • Next step after ABCDE-to-A

Notes

  • Triangular count. Total letters for n rows is still n(n+1)/2 - same as Program 1, only row order differs.
  • line += String.fromCharCode(code) stays on the line; console.log(line) advances - mix them carefully.
  • Validate rows > 0 for interactive programs; rows = 1 should print a single A (one row only).
  • This page shrinks from the top. Program 1 grows from the bottom - compare both to see how outer-loop direction drives the shape.

Quick Takeaway: outer loop counts down from rows, inner loop prints A..end, then break the line - mirror of Program 1.

⏱️ Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1-2)O(rows²)O(1)
letters.slice(0, i) (Example 3)O(rows²)O(rows) per row string (temporary)
Wrap Up

🎉 Conclusion

The decreasing alphabet pattern is a compact reverse-loop exercise with lasting payoff: for (let i = rows; i >= 1; i--), the same inner letter logic as Program 1, and O(n²) intuition. Master the classic two-loop version, then optionally shorten rows with letters.slice(0, i) inside the decreasing outer loop.

Practice the three examples above, then continue to Program 6 for the next left-trim variant in the series.

First row has rows letters - keep line += String.fromCharCode(code) for letters and console.log(line) for the break, and validate row counts when reading input.

💡 Best Practices

✅ Do

  • Explain outer = shrinking row length (rows down to 1), inner = A..end codes
  • Use for (let i = rows; i >= 1; i--) for the decreasing outer loop
  • Use line += String.fromCharCode(code) for letters and console.log(line) after each row
  • Validate rows ≥ 1 for interactive programs
  • Validate parseInt(prompt()) with Number.isFinite
  • State O(n²) time when asked about complexity

❌ Don’t

  • Call console.log(line) inside the inner letter loop
  • Use for (let i = 1; i <= rows; i++) when you meant the decreasing pattern
  • Skip the newline after each row
  • Ignore bad console input in user-facing demos
  • Skip the rows = 1 edge case

Key Takeaways

Knowledge Unlocked

Five things to remember about this decreasing pattern

Print ABCDE-to-A the beginner-friendly way.

5
Core concepts
02

Outer loop

for (let i = rows; i >= 1; i--)

Code
A 03

Inner loop

Prints A..end with print

Code
04

console.log(line)

Ends each row

I/O
O 05

Complexity

O(n²) time

Analysis

❓ Frequently Asked Questions

The outer loop walks i from rows down to 1, so the first row is longest. The inner loop appends letters from "A".charCodeAt(0) through i characters: ABCDE, then ABCD, then ABC, and so on until A.
That loop yields rows, rows-1, ..., 1 - exactly how many letters each row needs. Program 1 uses for (let i = 1; i <= rows; i++) for the opposite (growing) shape.
line += String.fromCharCode(code) builds the full row string. console.log() inside the inner loop would log one letter per line. Log once after the inner loop finishes.
Program 1 grows each row (A, AB, ABC). This pattern shrinks: the first row has rows letters from A, each next row drops the last letter. Only the outer loop direction changes - inner letter logic stays the same.
O(n^2) where n is the number of rows. Total logged characters still equal n+(n-1)+...+1 = n(n+1)/2 - same triangular count as the increasing triangle.
Yes. Keep letters = "ABCDEFG..." and console.log(letters.slice(0, i)) inside for (let i = rows; i >= 1; i--). Nested charCode loops teach the bounds; slice is a compact shortcut.
Use parseInt with Number.isFinite after prompt(), then clamp rows between 1 and 26 so letter codes stay within A-Z.
Letter codes walk past Z and log unexpected characters. Clamp to 26 for A-Z demos, or define a clear error/wrap policy.

Did you Know? 🔊

Outer loop for (let i = rows; i >= 1; i--) prints longest row first: ABCDE, ABCD, …, A. Inner loop still walks from A for i letters. Total letters for n rows is n(n+1)/2 - compare Program 1 (growing) and Program 6 (fixed end, shifting start).

Continue to Program 6

Shift the start letter each row for the next alphabet pattern in the series.

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