JavaScript Alphabet Diamond Pattern (With Stars)

Beginner
8 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

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 *.

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("*").

MethodIdeaBest for
j % 2 loopOdd j → letter; even j → *Learning, interviews, exams
fill + joinArray(i).fill(ch).join("*") builds the whole rowShorter 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

GoalPattern
Upper halffor (let i = 1; i <= n; i++)
Lower halffor (let i = n - 1; i >= 1; i--)
Row lengthfor (let j = 1; j < i * 2; j++) → 2i - 1 chars
Letter vs starline += (j % 2 === 0) ? "*" : ch;
Row letterString.fromCharCode("A".charCodeAt(0) + i - 1)
Join shortcutArray(i).fill(ch).join("*")
End the rowconsole.log(line);

Printing Letters vs Starting a New Line

APIEffectUse for
line += ch / "*"Stays on the same rowEach letter and each star
console.log(line)Ends the current rowAfter the inner loop

Append without a newline, then end the row once.

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 result n = 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

Worked Walkthrough — n = 3

Trace upper and lower halves. Peak row C*C*C appears once.

HalfiLetterCharsPrinted row
Upper1A1A
Upper2B3B*B
Upper3C5C*C*C
Lower2B3B*B
Lower1A1A

Odd widths 1 + 3 + … + (2n-1) = n² per half (peak once) → still O(n²) total.

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);
}
Try It Yourself

How It Works

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);
  }
}
Try It Yourself

How It Works

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);
}
Try It Yourself

How It Works

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.

Time and Space Complexity

ProgramTimeExtra 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.

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 *.

Next: Right-Aligned Sequential Pyramid

A running letter counter with fixed-width cells — A, then B C, then D E F…

Program 22 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful