JavaScript Alphabet Square Pattern (Symmetric Decreasing)

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

What Is This Pattern?

A symmetric decreasing alphabet square prints fixed-width mirrored rows whose interior floor drops from the top letter down to a single center A.

Remember
Rule: for floor i from k down to 0,
      left j = k..0 and right j = 1..k;
      print j if j > i, else print i

E E E E E E E E E
E D D D D D D D E
E D C C C C C D E
E D C B B B C D E
E D C B A B C D E     ← top = 'E' (k = 4, width 9)

The right half starts at j = 1 (B) so the center A is not duplicated. Program 29 reuses this row rule in two phases to form a full diamond.

How to Solve It

Two ways to emit the same square — inline floor checks, then optionally a shared helper for both halves.

MethodIdeaBest for
Inline j > iLeft k..0 + right 1..k with the same ternaryLearning, interviews, exams
Helper functionOne printCell owns the floor ruleCleaner demos once the rule clicks

Pseudocode

Pseudocode
k = top - 'A'
for i from k down to 0:
    for j from k down to 0:
        append (j > i ? alpha[j] : alpha[i]) + " "
    for j from 1 to k:
        append (j > i ? alpha[j] : alpha[i]) + " "
    print newline

Cheat sheet

GoalPattern
Top indexconst k = top.charCodeAt(0) - 65; (4 for E)
Drop the floorfor (let i = k; i >= 0; i--)
Left halffor (let j = k; j >= 0; j--) + j > i ? alpha[j] : alpha[i]
Right halffor (let j = 1; j <= k; j++) (skip 0)
End the rowconsole.log(line.trimEnd());
Row width2 * k + 1 (9 for A..E)
Avoid double AStart the right loop at 1, not 0

Printing Letters vs Starting a New Line

APIEffectUse for
line += …Stays on the same rowEach letter (and its trailing space)
console.log(line)Ends the current rowAfter both half-loops

Append cells without a newline, then end the row once.

Live Preview

Change the top letter and the layered square updates instantly — width is always 2k + 1.

One letter from A to F. Tap a chip or type a letter — the preview redraws as you go.

Live result Top E · 5 rows · width 9
E E E E E E E E E
E D D D D D D D E
E D C C C C C D E
E D C B B B C D E
E D C B A B C D E

JavaScript Programs

Three complete programs: fixed A–E, top-letter prompt, and a helper-function rewrite. Use View Output for sample results, or Try It Yourself to edit and run in the playground.

Example 1 — Fixed A–E

Two symmetric scans per row with the same j > i check.

JavaScript
const k = 4; // index for 'E'
const alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

for (let i = k; i >= 0; i--) {
  let line = "";
  for (let j = k; j >= 0; j--) {
    line += (j > i ? alpha[j] : alpha[i]) + " ";
  }
  for (let j = 1; j <= k; j++) {
    line += (j > i ? alpha[j] : alpha[i]) + " ";
  }
  console.log(line.trimEnd());
}
Try It Yourself

How It Works

1. Fix the top index. k = 4 means the highest letter is alpha[4] = E.

2. Drop the floor. Outer i runs from k down to 0 — one interior floor per row.

3. Left half. Scan j from k down to 0; append alpha[j] when j > i, else alpha[i].

4. Right half, then break. Scan j from 1 to k with the same rule, then console.log.

When i = 2 (floor C), columns where j > 2 print E/D borders, and interior cells print C.

Example 2 — Top Letter Input

Works for A..top with the same symmetric square. Prefer validating a single A–Z character.

JavaScript
const raw = (prompt("Enter top letter (like E):") || "").trim().toUpperCase();
const alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

if (!/^[A-Z]$/.test(raw)) {
  console.log("Please enter a single letter A-Z.");
} else {
  const k = raw.charCodeAt(0) - 65;

  for (let i = k; i >= 0; i--) {
    let line = "";
    for (let j = k; j >= 0; j--) {
      line += (j > i ? alpha[j] : alpha[i]) + " ";
    }
    for (let j = 1; j <= k; j++) {
      line += (j > i ? alpha[j] : alpha[i]) + " ";
    }
    console.log(line.trimEnd());
  }
}
Try It Yourself

How It Works

1. Prompt and scale. k = top.charCodeAt(0) - 65 sets both the floor loop and the two halves.

2. Same square core. Width becomes 2k + 1 (5 letters for top = C).

3. Same nested-loop core. Only the source of k changes — the print logic matches Example 1.

Example 3 — Helper Function

Often clearer to read: one function applies the floor rule so left and right loops stay thin.

JavaScript
function cell(alpha, j, i) {
  return (j > i ? alpha[j] : alpha[i]) + " ";
}

const k = 4;
const alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

for (let i = k; i >= 0; i--) {
  let line = "";
  for (let j = k; j >= 0; j--) {
    line += cell(alpha, j, i);
  }
  for (let j = 1; j <= k; j++) {
    line += cell(alpha, j, i);
  }
  console.log(line.trimEnd());
}
Try It Yourself

How It Works

1. Own the rule once. cell applies j > i in a single place.

2. Thin loops. Left and right loops only decide which columns to visit.

3. Same shape. Output matches Examples 1–2 — useful when you want to explain the floor rule separately.

Edge Cases & Pitfalls

Check these before calling the solution done.

j = 0

Duplicate center A

If the right half starts at 0, the last row prints A twice. Keep for (j = 1; j <= k; j++).

j >= i

Wrong borders

Using j >= i changes which cells belong to the floor vs the border. Stick to strict j > i.

log inside

Column of letters

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

top = A

Single A

When k = 0, output is just A — right half never runs. A good sanity check.

top < A

Empty / invalid

Validate A–Z before computing k.

Bad input

Validate one letter

Trim, uppercase, and require length 1 in A–Z — reject empty or multi-character prompts.

Time and Space Complexity

ProgramTimeExtra space
Inline loops (Examples 1–2)O(n²)O(n) for the current row string
Helper function (Example 3)O(n²)O(n) for the current row string

For n = k + 1 rows of width 2k + 1, total cells = n × (2n - 1) — still quadratic in n.

Key Takeaways

  • Rule: for floor i, append j > i ? alpha[j] : alpha[i] on both halves.
  • Mirror cleanly: left k..0, right 1..k — skip duplicating center A.
  • Break the row: call console.log only after both half-loops.
  • Complexity: O(n²) time; O(n) space for the current row string.

One line: for each floor i from top down to A, scan left k..0 and right 1..k, appending j > i ? alpha[j] : alpha[i], then console.log.

Frequently Asked Questions

It prints the border letters when the column letter j is above the current row floor i; otherwise it prints i. This builds higher-letter borders with a flat interior.
The first loop scans E down to A. The second scans B up to E so the middle A is printed once and the row is mirrored.
For letters A..k, width is (k-A+1) + (k-A) = 2*(k-A)+1. For A..E, width is 9.
Pick a larger top letter (like H), set k = topCharCode - base, and keep the same loop structure.
line += builds each letter and its trailing space on the same row. console.log inside either half-loop would log one cell per line. Log once after both halves finish.
O(n²) for n letters because there are n rows and each row prints O(n) cells.
Starting at 0 would print alpha[0] (A) again and duplicate the center. Starting at 1 (B) mirrors the left half cleanly.
Use prompt(), trim, toUpperCase(), require a single A–Z character, and reject empty or multi-character input.

Did you know?

Fix k at the top letter (E). Outer loop i goes from k down to A. Left half scans j = k..A; right half scans j = B..k so A appears once in the middle. Each position prints j when j > i, otherwise prints i.

Next: Reverse Centered Pyramid

Reuse this row rule in two phases to build a full diamond without duplicating the center.

Program 29 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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