JavaScript Alphabet Pattern (Decrease then Increase)
Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
Decreasing and increasing alphabet rows keep a fixed width: each row starts with a growing descending prefix, then fills the rest with a shrinking ascending suffix from A.
As the left side grows (B, CB, DCB, …), the right side shrinks (ABCD, ABC, AB, A) so every line stays the same length. Compare with Program 24 (palindrome triangles) and Program 29 (layered diamond).
Approach
How to Solve It
Two ways to emit the same grid — inline two loops per row, or factor a printRow helper.
Method
Idea
Best for
Two loops inline
Descending i..1, then ascending 0..(n−i)
Learning, interviews, exams
printRow helper
One function owns both parts; outer loop only picks i
Cleaner demos once the width budget clicks
Pseudocode
Pseudocode
n = end - 'A'
for i from 0 to n:
line = ""
for j from i down to 1: append alpha[j]
for k from 0 to n - i: append alpha[k]
print line
Cheat sheet
Goal
Pattern
End index
const n = end.charCodeAt(0) - 65; (4 for E)
Rows
for (let i = 0; i <= n; i++)
Descending prefix
for (let j = i; j > 0; j--) line += alpha[j];
Ascending suffix
for (let k = 0; k <= n - i; k++) line += alpha[k];
Append letters without a newline, then end the row once.
Try it
Live Preview
Change the end letter and the fixed-width decreasing/increasing grid updates instantly — every row has length n + 1.
One letter from A to F. Tap a chip or type a letter — the preview redraws as you go.
Live resultEnd E · 5 rows · width 5
ABCDE
BABCD
CBABC
DCBAB
EDCBA
Trace
Worked Walkthrough — End = E (n = 4)
Trace each row index, both parts, and the joined 5-letter line.
i
Prefix (i..1)
Suffix (0..4−i)
Printed row
0
(empty)
ABCDE
ABCDE
1
B
ABCD
BABCD
2
CB
ABC
CBABC
3
DCB
AB
DCBAB
4
EDCB
A
EDCBA
Width is always 4 + 1 = 5. The last row is a full reverse run ending at a single A.
Code
JavaScript Programs
Three complete programs: fixed A–E, end-letter prompt, and a helper-function rewrite. Use View Output for sample results, or Try It Yourself to edit and run in the playground.
Example 1 — Fixed A–E
Matches the reference logic: append alpha[i]…alpha[1], then alpha[0]…alpha[4 − i].
JavaScript
const alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (let i = 0; i <= 4; i++) {
let line = "";
for (let j = i; j > 0; j--) {
line += alpha[j];
}
for (let k = 0; k <= 4 - i; k++) {
line += alpha[k];
}
console.log(line);
}
Works for A..end with the same two-part row. Validate a single A–Z character from prompt.
JavaScript
const raw = (prompt("Enter end letter (like E):") || "").trim().toUpperCase();
const alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (!/^[A-Z]$/.test(raw)) {
console.log("Enter one letter from A to Z.");
} else {
const n = raw.charCodeAt(0) - 65;
for (let i = 0; i <= n; i++) {
let line = "";
for (let j = i; j > 0; j--) {
line += alpha[j];
}
for (let k = 0; k <= n - i; k++) {
line += alpha[k];
}
console.log(line);
}
}
1. Scale with n.n = endCode − 65 drives both loops. For end = C, width is 3 and you get three rows.
2. Same core. Only the end index changes; the prefix/suffix width budget is unchanged.
3. Safer input tip. Trim, uppercase, and require /^[A-Z]$/ — reject empty or multi-character prompts.
Example 3 — Helper Function
Often clearer: one function owns both parts so the outer loop only walks row indexes.
JavaScript
function printRow(alpha, n, i) {
let line = "";
for (let j = i; j > 0; j--) {
line += alpha[j];
}
for (let k = 0; k <= n - i; k++) {
line += alpha[k];
}
console.log(line);
}
const n = 4;
const alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (let i = 0; i <= n; i++) {
printRow(alpha, n, i);
}
1. printRow owns both parts. Prefix and suffix live in one place.
2. Thin outer loop. The caller only decides which row index i to print.
Edge Cases & Pitfalls
Check these before calling the solution done.
j >= 0
Duplicate A at the join
If the prefix includes index 0, you get …AA…. Keep for (j = i; j > 0; j--).
Wrong suffix end
Uneven row widths
Suffix must stop at n − i. Ending at a fixed n makes later rows too long.
log inside
Column of letters
If console.log is inside either loop, each letter lands on its own line. Append with +=; log only after both parts.
end = A
Single A
When n = 0, you print one row: A. A good sanity check.
i = 0
Empty prefix is normal
The first row prints only the ascending suffix A..end — that is expected, not a bug.
Bad prompt
Validate one letter
Trim, uppercase, and require length 1 in A–Z — reject empty or multi-character prompts.
Analysis
Time and Space Complexity
Program
Time
Extra space
Two loops inline (Examples 1–2)
O(n²)
O(n) for the current line string
Helper function (Example 3)
O(n²)
O(n) for the current line string
For letters A..end there are n + 1 rows, each of width n + 1 — quadratic in n.
Remember
Key Takeaways
Rule: prefix i..1 + suffix 0..(n − i).
Width budget: prefix grows as suffix shrinks — total always n + 1.
Break the row: call console.log(line) only after both loops.
Complexity:O(n²) time from (n+1)² letters.
One line: for each i, append i down to B, then A up through index n − i, then console.log.
Frequently Asked Questions
A is printed in the second loop. If the first loop included A, the join would duplicate A.
To keep each row the same width, end the ascending part at indices 0..(n − i). For A..E that is n − i with n = 4, so both parts always add up to n+1 letters.
Yes. Set n = end.charCodeAt(0) − "A".charCodeAt(0) and keep the same loops: descending j from i down to 1, then ascending k from 0 to n − i.
O(n²) for n letters because there are n+1 rows and each row prints O(n) characters.
The descending prefix has length i and the ascending suffix has length (n − i + 1). Together they always equal n + 1.
Trim and uppercase the prompt result, require a single A–Z character, and reject empty or multi-character input.
Program 24 builds palindrome triangles around A. This page keeps fixed-width rows by trading a growing descending prefix against a shrinking ascending suffix.
The descending loop does nothing, so you print only the ascending suffix A..end — for E that is ABCDE.
🤔
Did you know?
For each row i (A..E), print a descending prefix from i down to B (skip A), then print an ascending suffix from A up to index n − i. That cap keeps row width constant at n + 1.