Shape Rule
Vertical diamond
Widen 1..n, then mirror n-1..1 without duplicating the middle.

Build a vertical diamond where each row repeats the same letter, with * between letters. The pattern widens to the middle row, then mirrors back down — A, B*B, C*C*C, …, E*E*E*E*E, then back to A. Compare Program 15 (stars in the center) and Program 19 (mirrored letters with spaces). Includes a live preview, worked JavaScript examples, edge cases, and complexity.
Vertical diamond
Widen 1..n, then mirror n-1..1 without duplicating the middle.
One per row
Row i uses letter String.fromCharCode("A".charCodeAt(0) + i - 1) (or alpha[i-1]).
2i − 1
Inner loop prints 1, 3, 5, … characters per row.
j % 2
Odd positions print the letter; even positions print *.
Half height
Pick half height 1–8 and draw the diamond instantly.
Complexity
Odd-length rows up and down sum to O(n²).
A diamond alphabet pattern with stars repeats one letter on each row and places * between those letters, widening to a middle row and then mirroring back down.
In JavaScript you usually solve it with two outer loops (upper and lower halves) and an inner loop that uses j % 2 to choose letter vs star.
It teaches three classic ideas at once: odd row lengths, position-based alternation, and mirroring without duplicating the widest row.
Grow to n, then mirror from n-1.
Rows have 1, 3, 5, … characters.
Letter on odd, * on even.
Row i repeats letter number i.
In short: for each half-height row, append 2i-1 characters alternating letter and *, then mirror from n-1 down to 1.
Given a half height n (or fixed 5), print a vertical diamond of alternating letters and stars.
# Half height 5
# A
# B*B
# C*C*C
# D*D*D*D
# E*E*E*E*E
# D*D*D*D
# C*C*C
# B*B
# A | Item | Type | Description |
|---|---|---|
n | int | Half height (middle row letter = ‘A’ + n − 1). Cap at 26 for A–Z. |
| Printed output | text | About 2n-1 rows of letter/* patterns forming a diamond. |
for i in 1..n:
ch = 'A' + i - 1
line = ""
for j in 1..(2i-1):
line += (j even ? "*" : ch)
console.log(line)
for i in (n-1)..1:
(same inner loop) | Approach | Idea | Best for |
|---|---|---|
| Two outer halves | 1..n then n-1..1 | Matching this classic sample |
| Helper function | Extract “print row i” once | Avoiding duplicated inner loops |
| join rewrite | "*".join([ch]*i) style | JavaScriptic one-liner per row |
| Goal | Pattern |
|---|---|
| Upper half | for (let i = 1; i <= n; i++) |
| Lower half | for (let i = n - 1; i >= 1; i--) |
| Row length | for (let j = 1; j < i * 2; j++) → 2i-1 chars |
| Alternate | line += (j % 2 === 0 ? "*" : ch) |
| Row letter | ch = String.fromCharCode("A".charCodeAt(0) + i - 1) or alpha[i - 1] |
| End the row | console.log(line) |
Same row — different roles by column index.
letterPrints the current row letter (A, B, C…)
*Prints the separator between letters
widthOdd length so the row ends on a letter
breakEnds the row after the alternating run
Reach for this when teaching vertical mirrors and position-based alternation.
You already know half-and-mirror; now alternate symbols inside each row.
Practice j % 2 for clean letter/star placement.
Same 1, 3, 5… idea used in many pyramids and diamonds.
Replace * with - or spaces for variant labs.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one modulo check plus a careful lower-half start builds a clean vertical diamond.
Choose a half height between 1 and 8 and draw the diamond alphabet-and-stars pattern in the browser.
Three complete JavaScript programs - fixed half height 5, user-chosen half height, and a helper / join rewrite. Click View Output to reveal sample console results, or Try it Yourself to run in the browser editor.
Print the classic diamond with a letter string and two halves.
5Odd j appends the row letter; even j appends *. Upper half prints 1..5, lower half prints 4..1.
const alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (let i = 1; i <= 5; i++) {
let line = "";
for (let j = 1; j < i * 2; j++) {
line += (j % 2 === 0) ? "*" : alpha[i - 1];
}
console.log(line);
}
for (let i = 4; i >= 1; i--) {
let line = "";
for (let j = 1; j < i * 2; j++) {
line += (j % 2 === 0) ? "*" : alpha[i - 1];
}
console.log(line);
} When i = 3, the inner loop runs j = 1..5 and appends C * C * C. The lower half starts at 4 so E*E*E*E*E appears only once.
Let the user choose the half height.
Uses a computed row letter instead of an index into a string. Validate parseInt(prompt()) with Number.isFinite in real apps.
let n = parseInt(prompt("Enter half height (like 5):"), 10);
if (!Number.isFinite(n)) {
console.log("Please enter a whole number.");
} else {
n = Math.max(1, Math.min(n, 26));
const base = "A".charCodeAt(0);
for (let i = 1; i <= n; i++) {
const ch = String.fromCharCode(base + i - 1);
let line = "";
for (let j = 1; j < i * 2; j++) {
line += (j % 2 === 0) ? "*" : ch;
}
console.log(line);
}
for (let i = n - 1; i >= 1; i--) {
const ch = String.fromCharCode(base + i - 1);
let line = "";
for (let j = 1; j < i * 2; j++) {
line += (j % 2 === 0) ? "*" : ch;
}
console.log(line);
}
} Same alternation and mirror rules; only n changes the size. Math.max(1, Math.min(n, 26)) keeps row letters within A–Z.
Extract the row printer or use a join-based one-liner per row.
Same diamond, less duplicated code. The join version builds each row as ch + "*" + ch + ... in one expression.
function printRow(i) {
const ch = String.fromCharCode("A".charCodeAt(0) + i - 1);
console.log(Array(i).fill(ch).join("*"));
}
const n = 5;
for (let i = 1; i <= n; i++) {
printRow(i);
}
for (let i = n - 1; i >= 1; i--) {
printRow(i);
} Array(i).fill(ch).join("*") repeats the letter i times and inserts * between them - same visual as the j % 2 loop. The two outer loops only decide which row heights to print.
The first outer loop runs i = 1..n, making the row length grow.
Inner loop prints 2i-1 characters: odd j prints the letter, even j prints *.
Second outer loop runs i = n-1..1 so the widest row is not duplicated.
console.log(line) ends each row after the alternating run.
Total printed characters scale like O(n²) for half height n.
Trace each half and the characters printed on each row.
| Half | i | Letter | Chars (2i−1) | Printed row |
|---|---|---|---|---|
| Upper | 1 | A | 1 | A |
| Upper | 2 | B | 3 | B*B |
| Upper | 3 | C | 5 | C*C*C |
| Lower | 2 | B | 3 | B*B |
| Lower | 1 | A | 1 | A |
Total rows: 2n - 1 = 5. Middle row C*C*C appears once.
Where this diamond letter/star idea shows up beyond the homework prompt.
Clearest alphabet demo of j % 2 choosing two symbols.
Example: swap * for - and compare.
Practice starting the lower half at n-1.
Example: start at n once and see the doubled middle.
Refactor duplicated halves into print_row (Example 3).
Example: one function, two calling loops.
Add leading spaces later for a true 2D diamond silhouette.
Example: pad with n - i spaces before each row.
Odd sums up and down make O(n²) easy to see.
Example: n=5 prints 25 + 16 = 41 characters.
Practice limiting half height so letters stay in A–Z.
Example: reject n > 26 or clamp it.
Pro Tip: say “odd letter, even star, mirror from n minus one” before coding — that story prevents a doubled middle row.
Why this pattern earns a spot among diamond and separator labs.
Wrong modulo or a duplicated middle row shows up immediately.
One j % 2 check drives the whole letter/star effect.
A small helper or join rewrite removes duplicated upper/lower inner loops.
Streaming output needs no storage beyond loop variables.
Pro Tip: get the upper half right first; only then add the lower half starting at n-1.
Small habits that keep diamond letter/star code clean.
That single off-by-one avoids duplicating the widest row.
Use 2i-1 so every row ends on a letter, not a star.
Avoid crashes when the user types letters instead of a number.
Beyond Z you need a wrap/stop policy for row letters.
Share one inner loop or join expression between upper and lower halves.
Pro Tip: if the middle letter row appears twice, you almost certainly started the lower half at n instead of n-1.
Mistakes that commonly break diamond alphabet-and-star patterns.
Duplicates the widest row in the middle.
→ Start from n - 1.
Ending on a star breaks the letter-star-letter rhythm.
→ Print exactly 2i - 1 characters.
Printing stars on odd positions yields *B* instead of B*B.
→ Letter on odd j, star on even j.
Letters or empty input raise NaN.
→ Use Number.isFinite and re-prompt on failure.
Large half heights walk past the alphabet.
→ Cap n at 26 or define a wrap policy.
Check these inputs before calling the solution done.
Output is just A; lower half does not run.
Middle row is E*E*E*E*E.
Five rows through C*C*C.
Reject, clamp, or wrap — decide explicitly.
parseInt(prompt()) yields NaN — check Number.isFinite.
Same loops; only the even-position character changes.
Try these variations to lock in the pattern.
- instead of *n - i leading spacesn-1 so the widest row is not repeated.2i-1) so rows end on a letter.j → letter; even j → *.2n - 1 for half height n.Quick Takeaway: print odd-length letter/star rows from 1 to n, then mirror from n-1 to 1 — that is the whole diamond.
| Program | Time | Extra space |
|---|---|---|
| Fixed / input (Examples 1–2) | O(n²) | O(1) |
| Helper / join (Example 3) | O(n²) | O(1) |
Upper half prints about n² characters (sum of odds); lower half adds almost the same without the middle row — still O(n²).
The diamond alphabet-and-stars pattern is a small nested-loop exercise with lasting payoff: odd row lengths, position-based alternation, and a careful vertical mirror. Master the classic A…E…A sample, then try user input and a helper/join rewrite.
Practice the three examples above, then continue to Program 22’s right-aligned sequential alphabet pyramid.
Print 2i-1 characters with letter on odd positions and * on even ones, grow to n, then mirror from n-1.
n - 12i - 1)j, stars on even jNumber.isFinite and cap at 26n (duplicates middle)console.log(line) inside the alternating loopPrint the diamond alphabet-and-stars pattern the beginner-friendly way.
Widen then mirror
Definitionj % 2 letter/*
Code2i − 1 chars
CodeLower starts here
I/OO(n²) time
AnalysisUpper half prints rows 1..n; lower half prints n-1..1 so the widest row appears once. Each row runs j = 1..(2i-1). Odd j prints the row letter, even j prints *.
Next up: right-aligned sequential alphabet pyramids with a running letter counter.
12 people found this page helpful