Top Half
for (r = 0; r < rows; r++)
Same widening row rule as Program 33 - r = 0 .. rows-1.

An alphabet diamond mirrors the widening rows from Program 33: apex A, then B B, C C, up to the widest row E E, then the same rows in reverse down to A. The top half uses for (r = 0; r < rows; r++); the bottom half uses for (r = rows - 2; r >= 0; r--) so the widest row is not printed twice. Total lines: 2*rows - 1. This is the final program in the alphabet pattern series - next up: JavaScript Number Pattern Programs. Includes live preview, worked JavaScript examples, edge cases, and complexity.
for (r = 0; r < rows; r++)
Same widening row rule as Program 33 - r = 0 .. rows-1.
r = rows-2 .. 0
Mirror rows downward starting at rows - 2 - skips the widest row already printed.
String.fromCharCode(base + r)
ch = String.fromCharCode(base + r) picks the row letter - A on row 0, E on row 4 for half height 5.
2*r - 1
if r > 0: print (2 * r - 1) gap spaces and the same letter again - row 0 stays a single A.
1–26 half height
Pick half height and draw the full alphabet diamond in the browser instantly.
Total lines
Half height rows=5 prints 9 lines; widest row appears once - O(n²) time, O(1) extra memory.
An alphabet diamond combines the widening rows from Program 33 into a full symmetric shape: grow from centered A to the widest letter row, then shrink back to A without repeating the middle line. Row 0 prints a single A; each next row steps to the next letter - B B, C C, until the widest row, then the mirror runs in reverse.
In JavaScript you solve it with two loops sharing the same row logic: top half for (let r = 0; r < rows; r++), bottom half for (let r = rows - 2; r >= 0; r--) - or extract a printRow(r) function to DRY both passes.
It closes the alphabet pattern series by stacking Program 33’s top half with a mirrored bottom half - the same two-loop diamond pattern used in number diamonds, hollow pyramids, and symmetric ASCII art. After this, continue to JavaScript Number Pattern Programs.
" " * (rows - 1 - r) - row 0 gets rows - 1 spaces; bottom row gets none.
for (let r = 0; r < rows; r++) - identical row rule to Program 33.
for (let r = rows - 2; r >= 0; r--) - mirror without repeating the widest row.
2*rows - 1 output lines for half height rows - widest row printed once.
In short: set base = "A".charCodeAt(0), print top half with for (let r = 0; r < rows; r++), bottom half with for (let r = rows - 2; r >= 0; r--), using the same row rule - leading spaces, letter, optional gap and mirror - on every row.
Given a positive integer rows (half height), print a centered alphabet diamond of 2*rows - 1 lines. Row r prints (rows - 1 - r) leading spaces, then letter String.fromCharCode("A".charCodeAt(0) + r). For r > 0, print (2*r - 1) gap spaces and the same letter again. Top half: r = 0 .. rows-1. Bottom half: r = rows-2 .. 0.
// Half height rows = 5 (9 output lines)
A
B B
C C
D D
E E
D D
C C
B B
A | Item | Type | Description |
|---|---|---|
rows | int | Half height - apex to widest row (row letter runs A through the rows-th letter). Clamp to 1–26 for A–Z demos. |
| Printed output | text | Full alphabet diamond: 2*rows - 1 lines - widening rows up, then mirrored down without duplicating the widest row. |
base = "A".charCodeAt(0)
for r from 0 to rows-1: // top half
printRow(r)
for r from rows-2 down to 0: // bottom half
printRow(r)
printRow(r):
append (rows-1-r) leading spaces
ch = String.fromCharCode(base + r)
append ch
if r > 0: append (2*r-1) gap spaces and ch
console.log(line) | Approach | Idea | Best for |
|---|---|---|
| Two loops (inline row logic) | Top for (r = 0; r < rows; r++), bottom for (r = rows-2; r >= 0; r--) with duplicated print steps | Learning how diamond halves connect |
printRow(r) function | Extract shared row logic; call from both loops | Cleaner code and easier testing |
| Program 33 contrast | See Program 33 (top half only) | Understand what the diamond adds - the bottom mirror loop |
| Goal | Pattern |
|---|---|
| Leading spaces | line += " ".repeat(rows - 1 - r) |
| Top half loop | for (let r = 0; r < rows; r++) |
| Bottom half loop | for (let r = rows - 2; r >= 0; r--) |
| Total output lines | 2 * rows - 1 |
| Row letter | ch = String.fromCharCode(base + r) |
| Gap spaces (r > 0) | line += " ".repeat(2 * r - 1) |
| Row 0 guard | if r > 0: before gap and mirror |
| printRow helper | function printRow(r) { ... called from both loops |
Three ways to think about the diamond - the top grows, the bottom mirrors, and a helper DRYs both.
for (r = 0; r < rows; r++)
r = 0 .. rows-1Identical to Program 33 - widening rows from A to the widest letter.
for (r = rows-2; r >= 0; r--)
r = rows-2 .. 0Mirrors rows downward - starts at rows - 2 so the widest row is not printed twice.
function printRow(r) {
same row logicExtract shared logic - leading spaces, letter, gap, mirror - and call from both loops.
for (r = rows-1; r >= 0; r--)
duplicates widestStarting bottom at rows - 1 prints the widest row twice - use rows - 2 instead.
Reach for alphabet diamonds when closing Program 33’s triangle into a full symmetric shape - the capstone of the alphabet pattern series.
Program 33 prints the top half only. This program adds the bottom mirror loop to close the diamond.
Classic grow-then-shrink structure reused in number diamonds, star diamonds, and hollow pyramids.
Last alphabet pattern program - next section is JavaScript Number Pattern Programs.
Extract shared row logic into a function - both halves call the same helper.
This is a console teaching pattern - not how you build modern app screens.
Key benefit: one program that combines Program 33’s widening rows with a mirrored bottom half - the same two-loop diamond pattern used in number patterns, hollow pyramids, and symmetric ASCII art throughout the series.
Choose half height between 1 and 26 and draw the full alphabet diamond in the browser.
Three complete JavaScript programs - fixed half height with top and bottom loops, prompt input, and a printRow helper that DRYs both halves. Click View Output to reveal sample console results, or Try it Yourself to run in the browser editor.
Print a full alphabet diamond with half height 5 - top half then bottom mirror.
rows = 5 (half height)Hard-coded half height - ideal for first demos and screenshots.
let rows = 5;
rows = Math.max(1, Math.min(rows, 26));
const base = "A".charCodeAt(0);
// Top half: r = 0 .. rows-1
for (let r = 0; r < rows; r++) {
let line = "";
line += " ".repeat(rows - 1 - r);
const ch = String.fromCharCode(base + r);
line += ch;
if (r > 0) {
line += " ".repeat(2 * r - 1);
line += ch;
}
console.log(line);
}
// Bottom half: mirror without repeating the widest row
for (let r = rows - 2; r >= 0; r--) {
let line = "";
line += " ".repeat(rows - 1 - r);
const ch = String.fromCharCode(base + r);
line += ch;
if (r > 0) {
line += " ".repeat(2 * r - 1);
line += ch;
}
console.log(line);
} The first loop walks r from 0 to 4 - identical to Program 33. The second loop walks r from 3 down to 0, reusing the same row logic without printing the widest row (E E) again. Total output: 2*5 - 1 = 9 lines.
Let the user choose half height at runtime.
Read half height and clamp to 1–26. Validate parseInt(prompt(), 10) with Number.isFinite in real apps.
let rows = parseInt(prompt("Enter number of rows (half height, max 26):"), 10);
if (!Number.isFinite(rows)) {
console.log("Please enter a whole number.");
} else {
rows = Math.max(1, Math.min(rows, 26));
const base = "A".charCodeAt(0);
for (let r = 0; r < rows; r++) {
let line = "";
line += " ".repeat(rows - 1 - r);
const ch = String.fromCharCode(base + r);
line += ch;
if (r > 0) {
line += " ".repeat(2 * r - 1);
line += ch;
}
console.log(line);
}
for (let r = rows - 2; r >= 0; r--) {
let line = "";
line += " ".repeat(rows - 1 - r);
const ch = String.fromCharCode(base + r);
line += ch;
if (r > 0) {
line += " ".repeat(2 * r - 1);
line += ch;
}
console.log(line);
}
} Same diamond core as Example 1; only half height comes from prompt. Three half-height rows produce 5 output lines: top 3 widening rows plus bottom 2 mirrored rows (starting at r = 1).
Extract shared row logic into a function - both loops call the same helper.
printRow(r) Function VariantDRY the two loops by extracting identical row logic into one function.
let rows = 5;
rows = Math.max(1, Math.min(rows, 26));
const base = "A".charCodeAt(0);
function printRow(r) {
let line = "";
line += " ".repeat(rows - 1 - r);
const ch = String.fromCharCode(base + r);
line += ch;
if (r > 0) {
line += " ".repeat(2 * r - 1);
line += ch;
}
console.log(line);
}
for (let r = 0; r < rows; r++) {
printRow(r);
}
for (let r = rows - 2; r >= 0; r--) {
printRow(r);
} printRow(r) encapsulates leading spaces, letter, gap, and mirror - both loops simply call it with different r ranges. Produces identical output to Examples 1 and 2, but easier to test and maintain.
Clamp rows (half height), then set base = "A".charCodeAt(0) for the alphabet starting point.
for (let r = rows - 2; r >= 0; r--) - mirror rows without repeating the widest line.
Each row: leading spaces, letter, if r > 0 gap and mirror - extract as printRow(r) to DRY both loops.
2*rows - 1 output lines for half height rows - O(n²) time, O(1) extra memory (loop version).
rows = 5 (half height)Trace each value of r through top and bottom phases - top r = 0..4 matches Program 33; bottom starts at r = 3, 2, 1, 0.
| phase | r | letter | full row |
|---|---|---|---|
| top | 0 | A | A |
| top | 1 | B | B B |
| top | 2 | C | C C |
| top | 3 | D | D D |
| top | 4 | E | E E |
| bottom | 3 | D | D D |
| bottom | 2 | C | C C |
| bottom | 1 | B | B B |
| bottom | 0 | A | A |
Highlight: top r = 0..4 is identical to Program 33. Bottom starts at r = 3 (rows - 2), not r = 4 - starting at rows - 1 would duplicate the widest row E E. Total: 9 lines = 2*5 - 1.
Where alphabet diamonds show up - closing the alphabet pattern series before number patterns.
Program 33 is the top half only. This program adds the bottom mirror to close the diamond.
Example: run Program 33 output, then append rows for r = rows-2 .. 0.
Last program in the alphabet pattern series - next up is JavaScript Number Pattern Programs.
Example: compare this diamond with number diamond patterns in the next section.
Extract shared row logic - both halves call the same function with different r ranges.
Example: test printRow(2) in isolation before wiring both loops.
Bottom loop must start at rows - 2, not rows - 1.
Example: for rows=5, bottom starts at r=3 - skipping r=4 prevents double E E.
2*rows - 1 lines with O(n) chars each - O(n²) total.
Example: half height 5 → 9 output lines, not 10.
Classic two-loop diamond question - explain why bottom starts at rows-2.
Example: explain half height vs total lines without running code.
Pro Tip: say “top half Program 33, bottom half rows-2 down to 0” before coding - that story prevents duplicating the widest row.
Why this pattern earns a spot as the alphabet pattern series finale.
Top half is Program 33; bottom half completes the symmetric diamond.
Classic grow-then-shrink structure reused across number and star patterns.
Function extraction keeps both halves readable and testable.
Streaming output needs no storage beyond loop counters.
Pro Tip: when bottom loop starts at rows - 1, you get two widest rows - start at rows - 2 instead.
Small habits that keep alphabet diamond code clean.
for (r = rows - 2; r >= 0; r--) - never rows - 1 or the widest row prints twice.
Both loops share identical logic - a helper prevents copy-paste bugs.
rows = Math.max(1, Math.min(rows, 26)) keeps demos inside A–Z.
if r > 0: must wrap gap and mirror - never print a second A on row 0.
Trace 5 output lines: top A, B B, C C plus bottom B B, A.
Pro Tip: if gaps look too narrow, check the formula - it should be 2*r - 1, not 2*r.
Mistakes that commonly break alphabet diamonds.
Using for (let r = rows - 1; r >= 0; r--) duplicates the widest row - you get two middle lines.
→ Start bottom at rows - 2: for (let r = rows - 2; r >= 0; r--)
Forgetting if r > 0 prints A A on row 0 - two letters at the apex.
→ Wrap gap and mirror in if r > 0: so row 0 prints only one A.
Using 2*r instead of 2*r - 1 makes gaps one space too wide starting at row 1.
→ Use 2*r - 1 for the gap - row 1 needs 1 space, row 4 needs 7.
Using r lead spaces or rows - r misaligns the triangle - rows lean or over-indent.
→ Use rows - 1 - r leading spaces so row 0 gets the most padding.
Expecting 2*rows output lines instead of 2*rows - 1 - the widest row appears once.
→ Half height rows=5 prints 9 lines, not 10.
Check these inputs before calling the solution done.
Output is just A - one line diamond; bottom loop for (r = -1; r >= 0; r--) never runs.
Treat as invalid; re-prompt instead of silent empty output.
26 half height with letter Z at widest row - 51 total output lines.
Clamp to 26 or define a wrap/error policy before printing.
Use Number.isFinite before clamping rows.
Same loops work with base = "a".charCodeAt(0) and lowercase output.
Try these variations to lock in the pattern.
printRow(r) from Example 3rows produces 2*rows - 1 output lines - widest row printed once.for (r = rows - 2; r >= 0; r--) - starting at rows - 1 duplicates the widest row.printRow(r) is equivalent to inline row logic - use whichever fits your lesson.Quick Takeaway: top half for (r = 0; r < rows; r++), bottom half for (r = rows - 2; r >= 0; r--), same row rule on every line - that is the whole alphabet diamond.
| Program | Time | Extra space |
|---|---|---|
| Two loops inline (Examples 1–2) | O(rows²) | O(1) |
| printRow function (Example 3) | O(rows²) | O(1) - function call overhead only |
The alphabet diamond combines Program 33’s widening top half with a mirrored bottom half - two loops, one row rule, 2*rows - 1 total lines. Master the inline two-loop version, then try the printRow(r) helper for cleaner code.
This closes the alphabet pattern series. Practice the three examples above, then continue to JavaScript Number Pattern Programs.
Top half for (r = 0; r < rows; r++), bottom half for (r = rows - 2; r >= 0; r--), guard row 0 with if r > 0, clamp rows to 26, and compare with Program 33 (widening triangle).
base = "A".charCodeAt(0), clamp half height rows to 1–26for (let r = 0; r < rows; r++) with Program 33 row logicfor (let r = rows - 2; r >= 0; r--)if r > 0: gap 2*r-1 and mirror chprintRow(r) to DRY both loopsrows - 1 - duplicates widest row2*rows - 1"A".charCodeAt(0)2*r for gap - gaps one space too widePrint the full diamond the beginner-friendly way - last program in the alphabet pattern series.
for (r = 0; r < rows; r++)
Growfor (r = rows-2; r >= 0; r--)
Shrink2*rows - 1
CountDRY both loops
CodeO(n²) time
AnalysisThe top half uses r = 0 .. rows-1 with the same row rule as Program 33. The bottom half reuses that logic for r = rows-2 .. 0 so the widest row is not printed twice. Total output lines: 2*rows - 1.
You've completed the alphabet pattern series - next up: number patterns with the same diamond and pyramid ideas.
12 people found this page helpful