JavaScript Diamond Alphabet Pattern

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

What Is This Pattern?

A diamond alphabet pattern is a hollow letter outline: an inverted V from Program 33, then a mirrored lower half that closes under the waist.

Remember
Rule: same row recipe twice
      upper i = 0..n, then lower i = n−1..0
      (letter only when i == j or i == k)

    A    
   B B   
  C   C  
 D     D 
E       E
 D     D 
  C   C  
   B B   
    A         ← A–E (width 9, height 9)

Geometry matches the hollow diamond in Star Pattern 9, but cells print letters instead of *. The only new idea beyond Program 33 is skipping i = n on the way down so the widest line is not printed twice.

How to Solve It

Reuse one row printer for both halves — expand with if/else first, then optionally share a cell helper.

MethodIdeaBest for
Shared printRowUpper 0..n, then lower n-1..0, same diagonalsLearning, interviews, exams
Helper + ternaryOne cell(row, col) used by both legsLess duplication once the diagonals click

Pseudocode

Pseudocode
n = endLetter - 'A'          // 4 when end is 'E'

function printRow(i):
    for j from n down to 0:
        print alpha[j] if i == j else " "
    for k from 1 to n:
        print alpha[k] if i == k else " "
    print newline

for i from 0 to n:            // upper + waist
    printRow(i)
for i from n - 1 down to 0:   // lower (skip waist)
    printRow(i)

Cheat sheet

GoalPattern
Scale from end letterconst n = end.charCodeAt(0) - 65;
Upper half (incl. waist)for (let i = 0; i <= n; i++)
Lower half (no waist)for (let i = n - 1; i >= 0; i--)
Left diagonalfor (let j = n; j >= 0; j--) + if (i === j)
Right diagonalfor (let k = 1; k <= n; k++) + if (i === k)
Width / height2 * n + 1 columns and rows
End the rowconsole.log(line);
Upper half aloneProgram 33

Printing Letters vs Starting a New Line

APIEffectUse for
line += …Stays on the same lineEach letter and each space
console.log(line)Ends the current lineAfter both inner loops

Append cells without a newline, then end the row once — for both halves.

Live Preview

Change the end letter and the diamond updates instantly — including height and letter count.

One letter from A to Z. Height and width are both 2 * (end - 'A') + 1.

Live result A–E · 9 rows · 16 letters
    A    
   B B   
  C   C  
 D     D 
E       E
 D     D 
  C   C  
   B B   
    A    

Worked Walkthrough — A–E (n = 4)

Upper half is Program 33. Lower half reuses the same row body with decreasing i (line width = 9).

HalfiLettersPrinted row
Upper01A
Upper12B B
Upper22C C
Upper32D D
Waist42E E
Lower32D D
Lower22C C
Lower12B B
Lower01A

Lower i never equals 4 again — that keeps a single waist. Outline letters: 1 + 2×4 + 2×3 + 1 = 16 = 4×4 for n ≥ 1.

JavaScript Programs

Three complete programs: fixed A–E diamond, end-letter prompt, and a reusable cell helper. Use View Output or Try It Yourself to explore.

Example 1 — Fixed A–E

Upper half through the waist, then lower half from n - 1. Same diagonal rule in both.

JavaScript
const alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const n = 4; // A–E

function printRow(i) {
  let line = "";
  for (let j = n; j >= 0; j--) {
    if (i === j)
      line += alpha[j];
    else
      line += " ";
  }
  for (let k = 1; k <= n; k++) {
    if (i === k)
      line += alpha[k];
    else
      line += " ";
  }
  console.log(line);
}

for (let i = 0; i <= n; i++) {
  printRow(i);
}
for (let i = n - 1; i >= 0; i--) {
  printRow(i);
}
Try It Yourself

How It Works

1. Shared row. printRow(i) builds left then right diagonals for one value of i.

2. Climb. i = 0..4 draws tip through waist — same as Program 33.

3. Descend. i = 3..0 mirrors without reprinting E E.

4. Break the row. console.log(line) runs once after both inner loops — never inside them.

Example 2 — End Letter Input

Read the end letter and scale both halves with n = end - 'A'. Prefer validating a single A–Z character in real apps.

JavaScript
const raw = (prompt("Enter end letter (like E):") || "").trim().toUpperCase();
const end = raw[0];
const n = end.charCodeAt(0) - 65;
const alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

function printRow(i) {
  let line = "";
  for (let j = n; j >= 0; j--)
    line += i === j ? alpha[j] : " ";
  for (let k = 1; k <= n; k++)
    line += i === k ? alpha[k] : " ";
  console.log(line);
}

for (let i = 0; i <= n; i++)
  printRow(i);
for (let i = n - 1; i >= 0; i--)
  printRow(i);
Try It Yourself

How It Works

1. Prompt and normalize. Read a string, trim it, and take the first character as uppercase.

2. Scale both phases. For end = C, n = 2 — height and width are both 5.

3. Safer input tip. Bare [0] fails on empty input. Prefer:

Safer input
const raw = (prompt("Enter end letter (like E):") || "").trim().toUpperCase();
if (raw.length !== 1 || raw < "A" || raw > "Z") {
  console.log("Enter a single letter A–Z.");
} else {
  const end = raw;
  // … build diamond with n = end.charCodeAt(0) - 65
}

Example 3 — Helper Function

Extract one cell printer so both diagonal loops — and both halves — stay thin.

JavaScript
function cell(alpha, row, col) {
  return row === col ? alpha[col] : " ";
}

const n = 4;
const alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

function printRow(i) {
  let line = "";
  for (let j = n; j >= 0; j--)
    line += cell(alpha, i, j);
  for (let k = 1; k <= n; k++)
    line += cell(alpha, i, k);
  console.log(line);
}

for (let i = 0; i <= n; i++)
  printRow(i);
for (let i = n - 1; i >= 0; i--)
  printRow(i);
Try It Yourself

How It Works

1. One cell rule. cell owns the row === col decision and the space fallback.

2. Same bounds. Left still counts down from n; right still starts at 1.

3. Same shape, less copy-paste. Learn the expanded if/else first (Example 1), then refactor when both halves feel familiar.

Edge Cases & Pitfalls

Check these before calling the solution done.

Lower starts at n

Double waist

If the second outer loop starts at i = n, the widest line prints twice. Use n - 1.

k = 0

Duplicate tip A

Starting the right loop at k = 0 prints two As on tip rows. Keep k = 1.

log inside

Broken outline

If console.log is inside either inner loop, each cell lands on its own line. Append with +=; log only after both loops.

end = A

Single tip

Upper prints one line; lower never runs. Output is just A — a good sanity check.

Proportional font

Looks skewed in the IDE

Spaces and letters need a monospace font. Proportional fonts make diagonals look uneven.

Bad input

Validate one letter

Empty strings and multi-character input break naive [0] indexing — require a single A–Z character.

Time and Space Complexity

ProgramTimeExtra space
Shared printRow (Examples 1–2)O(n²)O(1) beyond the alphabet string
Helper function (Example 3)O(n²)O(1) beyond the alphabet string

Lines printed = 2n + 1. Each line walks about 2n + 1 positions across the two inner loops, so work is still quadratic in n. Outline letters grow as 4n for n ≥ 1 (and 1 when n = 0).

Key Takeaways

  • Compose halves: upper 0..n, then lower n-1..0 — Program 33 with one seam fix.
  • Diagonal rule: letter only when i === j or i === k; everything else is a space.
  • Break the row: call console.log(line) only after both inner loops.
  • Complexity: O(n²) time; O(1) extra space beyond the alphabet string.

One line: print hollow alphabet rows for i = 0..n, then again for i = n-1..0, starring only the diagonals.

Frequently Asked Questions

Program 33 prints only the upper inverted V (i from 0 to n). Program 34 reuses that row recipe, then walks i from n−1 down to 0 so the outline closes into a diamond without repeating the waist.
Row i = n is already printed at the end of the upper half. Starting again at n would duplicate the widest line (two E rows for A–E).
With n = 4, each row has width 2n + 1 = 9. Total rows are 2n + 1 = 9 (five upper including the waist, then four mirrored below).
line += appends a letter or space on the same row. console.log(line) ends the current line after both inner loops finish.
O(n²). There are 2n + 1 rows and each row scans about 2n + 1 positions across the left and right blocks.
Spaces keep column alignment so only the diagonal outline shows. Without them, letters collapse left and the diamond disappears.
n is 0. The upper half prints one row with A; the lower loop never runs. Output is a single tip — a useful edge-case check.
Use prompt, trim and toUpperCase, require a single A–Z character, and reject empty or multi-character input before indexing.

Did you know?

The diamond is just Program 33 plus a second pass of the same row logic from n - 1 down to 0. Starting the lower half at n would print the widest row twice — that single off-by-one is the most common bug.

Next: Number Patterns

Move from alphabet outlines to number-based pattern tutorials.

Number patterns →

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