A diamond alphabet pattern with stars widens from A to a middle row like E*E*E*E*E, then mirrors back down. Each row repeats one letter with * between copies.
Remember
Rule: row i = letter i joined by * (2i-1 chars); mirror after peak
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 ← half height n = 5
Upper half prints i = 1..n; lower half prints n-1..1 so the widest row appears once. Odd positions hold the letter; even positions hold *.
Approach
How to Solve It
Print a growing half, then a shrinking half. Build each row with j % 2 or with Array(i).fill(ch).join("*").
Method
Idea
Best for
j % 2 loop
Odd j → letter; even j → *
Learning, interviews, exams
fill + join
Array(i).fill(ch).join("*") builds the whole row
Shorter demos once loops click
Pseudocode
Pseudocode
n = half height (e.g. 5)
for i from 1 to n: // upper
print row of letter i with * between
for i from n-1 down to 1: // lower (skip peak)
print row of letter i with * between
// one row:
for j from 1 to 2*i - 1:
if j is even: append '*'
else: append letter for i
Cheat sheet
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
Letter vs star
line += (j % 2 === 0) ? "*" : ch;
Row letter
String.fromCharCode("A".charCodeAt(0) + i - 1)
Join shortcut
Array(i).fill(ch).join("*")
End the row
console.log(line);
Printing Letters vs Starting a New Line
API
Effect
Use for
line += ch / "*"
Stays on the same row
Each letter and each star
console.log(line)
Ends the current row
After the inner loop
Append without a newline, then end the row once.
Try it
Live Preview
Change the half height and the diamond updates instantly — including peak letter and total rows.
Whole numbers from 1 to 10. Size 5 peaks at E*E*E*E*E. Tap a chip or type a value — the preview redraws as you go.
Live resultn = 5 · peak E · 9 rows
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
Trace
Worked Walkthrough — n = 3
Trace upper and lower halves. Peak row C*C*C appears once.
Half
i
Letter
Chars
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
Odd widths 1 + 3 + … + (2n-1) = n² per half (peak once) → still O(n²) total.
Code
JavaScript Programs
Three complete programs: fixed n = 5, half-height prompt, and a printRow / join helper. Use View Output for sample results, or Try It Yourself to edit and run in the playground.
Example 1 — Fixed Half Height 5
Odd j appends the row letter; even j appends *. Upper half prints 1..5, lower half prints 4..1.
JavaScript
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);
}
1. Upper half grows.i runs 1..5; row letter is alpha[i - 1].
2. Alternate with j % 2. Inner loop runs j = 1..(2i-1): even → *, odd → letter.
3. Lower half shrinks. Start at 4 so E*E*E*E*E appears only once.
When i = 3, j = 1..5 builds C*C*C.
Example 2 — Half Height Input
Uses a computed row letter instead of an index into a string. Validate and clamp to 26 for A–Z.
JavaScript
let n = parseInt(prompt("Enter half height (like 5):"), 10);
if (!Number.isFinite(n) || n < 1) {
console.log("Please enter a whole number >= 1.");
} else {
if (n > 26) 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);
}
}
1. Prompt and validate. Parse with parseInt, reject invalid values, clamp to 26.
2. Same alternation and mirror. Only n changes the size — the j % 2 and lower-half rules match Example 1.
Example 3 — printRow Helper / join
Same diamond, less duplicated code. The join version builds each row as ch + "*" + ch + ... in one expression.
JavaScript
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);
}
1. One row builder.Array(i).fill(ch).join("*") repeats the letter i times and inserts * between them.
2. Halves only pick heights. The two outer loops decide which i values to print — same visual as the j % 2 loop.
Edge Cases & Pitfalls
Check these before calling the solution done.
double peak
Lower half starts at n
Starting lower at n duplicates the widest row. Use n - 1.
parity flip
Swapped letter / star
If you treat even as letter, you get *B* shapes. Odd j must be the letter when j starts at 1.
wrong length
Even-width rows
Loop to 2*i - 1 (odd count). An even length cannot end on a letter after alternating from a letter start.
log early
One symbol per line
Call console.log only after the inner loop finishes building the full row.
n = 1
Single A
Output is just A; the lower half does not run — a good sanity check.
Bad prompt
Validate and clamp
Reject NaN / non-positive values; cap at 26 so letters stay in A–Z.
Analysis
Time and Space Complexity
Program
Time
Extra space
j % 2 loops (Examples 1–2)
O(n²)
O(n) for the current line string
fill + join (Example 3)
O(n²)
O(n) for each row array / string
Odd widths sum to n² on the upper half (and almost that again below), so total work is quadratic in half height n.
Remember
Key Takeaways
Rule: row i is letter i repeated i times with * between.
Mirror: upper 1..n, lower n-1..1 — peak once.
Alternate: odd j → letter; even j → *.
Complexity:O(n²) from odd-width rows up and down.
One line: grow rows with letter/* alternation, then shrink from n-1 without repeating the peak.
Frequently Asked Questions
j runs 1,2,3,… Odd positions print the row letter and even positions print '*', producing lines like B*B and C*C*C.
The inner loop checks the position: even columns print '*', odd columns print the row letter. That alternates symbols cleanly.
That prints 2*i-1 characters per row (1,3,5,…), which makes the pattern widen toward the center.
The upper half already printed the widest row at i=n. Starting from n-1 mirrors without duplicating the middle row.
line += ch or line += '*' stays on the same row for each letter or star. console.log(line) ends the row after the inner loop finishes.
Program 15 puts stars in the center of a symmetric alphabet row. This pattern repeats one letter per row and places stars between those letters, then mirrors vertically.
O(n²) for half height n because the total printed characters is proportional to 1+3+…+(2n-1) up and down.
Use parseInt(prompt(...), 10), check Number.isFinite(n), require n >= 1, and cap at 26 so row letters stay within A–Z.
🤔
Did you know?
Upper 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 *.