Shape Rule
A..end letter, shrinking rows
Row 1 prints ABCDE (all rows letters), then ABCD, …, down to a single A.

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.
A..end letter, shrinking rows
Row 1 prints ABCDE (all rows letters), then ABCD, …, down to a single A.
Rows
for (let i = rows; i >= 1; i--) picks how many letters each row prints - longest first.
Letters
for (let code = base; code < base + i; code++) still prints letters from A; only the outer bound i shrinks each row.
Same line / next line
Letters use line += String.fromCharCode(code); end each row with console.log(line).
1–26 rows
Pick a row count and draw the decreasing alphabet pattern instantly in the browser.
Complexity
Total letters = n(n+1)/2; extra memory stays O(1).
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.
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.
On row i, print i letters from A; first row has rows letters.
for (let i = rows; i >= 1; i--) walks longest row first.
line += String.fromCharCode(code) in the inner loop; console.log(line) after.
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).
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.
// First 5 rows (conceptual shape)
// ABCDE
// ABCD
// ABC
// AB
// A | Item | Type | Description |
|---|---|---|
rows | int | Number of triangle lines to print (typically ≥ 1). |
| Printed output | text | Left-aligned rows of letters; first row has rows letters from A, each row one shorter. |
for i from rows down to 1:
for j from 1 to i:
print next letter from A (no newline)
print newline | Approach | Idea | Best for |
|---|---|---|
| Nested loops | Decreasing outer + inner letters from A | Learning and interviews |
| Reverse outer loop | for (let i = rows; i >= 1; i--) | Decreasing row lengths - this pattern |
letters.slice(0, i) | Slice first i letters with decreasing i | Shorter production-style demos |
| Goal | Pattern |
|---|---|
| Walk each row (decreasing) | for (let i = rows; i >= 1; i--) |
Print A..end letters | for (let code = base; code < base + i; code++) { line += String.fromCharCode(code); } |
| End the row | console.log(line) |
| One-line row shortcut | console.log(letters.slice(0, i)) inside decreasing outer loop |
| Growing variant | Use for (let i = 1; i <= rows; i++) - see Program 1 |
Same decreasing pattern - different ways to emit characters.
same linePrints a letter without moving to the next line
new lineEnds the current row after all letters are printed
whole rowBuilds letters A..end at once - skip the inner loop
loops firstMaster nested loops before the string shortcut
Reach for this pattern when teaching reverse outer loops or mirroring Program 1’s growing triangle.
Natural follow-up after Program 1 - same inner logic, outer loop counts down.
Practice for (let i = rows; i >= 1; i--) with an immediate visual check.
Combine loops with prompt() for a flexible row count.
Leads to left-trim patterns, pyramids, and hollow shapes in the series.
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.
Choose a row count between 1 and 20 and draw the decreasing alphabet pattern in the browser.
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.
Print five rows with classic nested loops - longest row first.
rows = 5Hard-coded height - ideal for first demos and screenshots.
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);
} 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.
Let the user choose the height at runtime.
Read the row count with prompt() and convert with parseInt() (validate with Number.isFinite in real apps).
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);
} 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.
Same shape without an explicit inner letter loop.
letters.slice(0, i)Slice A-Z for each shrinking row length with letters.slice(0, i) inside a decreasing outer loop.
const rows = 5;
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (let i = rows; i >= 1; i--) {
console.log(letters.slice(0, i));
} 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.
Use prompt() when reading input. Set rows (fixed or from user) and clamp to 1-26 for A-Z.
for (let i = rows; i >= 1; i--) selects how many letters the current line prints - longest first.
for (let code = base; code < base + i; code++) prints each letter with line += String.fromCharCode(code).
console.log(line) ends the row so the next outer iteration starts fresh.
Total letters: n+(n-1)+…+1 = n(n+1)/2 — O(n²) time, O(1) extra memory.
rows = 4Trace each outer-loop value i (counting down) and see what the inner loop prints from A.
Outer i | Inner code range | Printed row | Letters this row |
|---|---|---|---|
4 | A..D | ABCD | 4 |
3 | A..C | ABC | 3 |
2 | A..B | AB | 2 |
1 | A..A | A | 1 |
Total letter prints: 4 + 3 + 2 + 1 = 10 = 4×5/2. Same triangular total as Program 1 - only row order differs.
Where this shrinking letter pattern (and its reverse outer loop) shows up beyond the homework prompt.
Clearest visual proof that for (let i = rows; i >= 1; i--) shrinks row length each iteration.
Example: compare side-by-side with Program 1.
Natural step after Program 1 before left-trim and pyramid letter patterns.
Example: Program 6 shifts the start letter each row.
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.
Swap to lowercase or digits once the letter loop works.
Example: print lowercase a..z once uppercase clicks.
Triangular totals make O(n²) concrete for beginners.
Example: count printed letters for n = 10 → 55.
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.
Why this decreasing pattern earns a spot after Program 1 in beginner JavaScript courses.
Side-by-side with Program 1 makes reverse outer loops obvious.
Only loops and console output - no arrays or math libraries.
One-line outer-loop change flips between growing and shrinking shapes.
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.
Small habits that keep decreasing alphabet-pattern code clean.
Use rows (or n) and keep i/j for row/column - or rename to row/col.
parseInt(prompt()) with Number.isFiniteAvoid crashes when the user types letters instead of a number.
Only call console.log(line) after the inner loop finishes the row.
for (let i = rows; i >= 1; i--) matches “first row longest, each row one shorter” naturally.
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.
Mistakes that commonly break decreasing alphabet patterns.
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.
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--).
Omitting console.log(line) after the inner loop glues every letter onto one endless line.
→ Always end the row after the inner loop.
Non-numeric input yields NaN with bare parseInt(prompt()).
→ Check with Number.isFinite and clamp the range.
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++).
Check these inputs before calling the solution done.
Output ends with just A on the last line.
Outer loop never runs - print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Output grows as n²/2 characters - fine for labs, noisy for huge n.
parseInt(prompt()) yields NaN - validate first.
Same loops work with #, digits, or letters.
Try these variations to lock in the decreasing pattern.
1 to rowsString.fromCharCode(code) with digit logicNumber.isFinite until rows >= 1n(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.rows > 0 for interactive programs; rows = 1 should print a single A (one row only).Quick Takeaway: outer loop counts down from rows, inner loop prints A..end, then break the line - mirror of Program 1.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1-2) | O(rows²) | O(1) |
letters.slice(0, i) (Example 3) | O(rows²) | O(rows) per row string (temporary) |
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.
rows down to 1), inner = A..end codesfor (let i = rows; i >= 1; i--) for the decreasing outer loopline += String.fromCharCode(code) for letters and console.log(line) after each rowrows ≥ 1 for interactive programsparseInt(prompt()) with Number.isFiniteconsole.log(line) inside the inner letter loopfor (let i = 1; i <= rows; i++) when you meant the decreasing patternrows = 1 edge casePrint ABCDE-to-A the beginner-friendly way.
First row longest, then shrink
Definitionfor (let i = rows; i >= 1; i--)
CodePrints A..end with print
CodeEnds each row
I/OO(n²) time
AnalysisShift the start letter each row for the next alphabet pattern in the series.
12 people found this page helpful