Shape Rule
Layered square
Borders stay high; interiors drop toward A.

Stay symmetric by printing a left half (E down to A) and a mirrored right half (B up to E). Each cell follows the same rule: if j > i print the column letter; otherwise print the current row floor i. Compare Program 21 (diamond symmetry) and Program 24 (palindrome triangles). Includes a live preview, worked JavaScript examples, edge cases, and complexity.
Layered square
Borders stay high; interiors drop toward A.
Mirror
Left E..A, right B..E — A once in the center.
j > i
Print column letter or row floor letter.
2k+1
For A..E (k=4), every row has 9 letters.
Top letter
Pick a top letter (A–F) and draw the layers.
Complexity
n rows × O(n) cells each.
A symmetric decreasing alphabet square prints fixed-width rows whose outer letters stay high while the interior floor drops from the top letter down to A, mirrored left and right.
In JavaScript you solve it with an alphabet string, a descending row floor, and two column scans that share the same j > i choice rule.
It teaches mirrored scans, a shared cell rule, and how to keep a single center letter — skills that transfer to concentric squares and Program 29.
Drops E → A each row.
Left half + right half.
Border vs interior choice.
Right half starts at B.
In short: 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 call console.log(line).
Given a top letter (or fixed E), print a fixed-width symmetric square whose interior floor drops from the top letter down to A.
// Five rows (space after each letter; 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 | Item | Type | Description |
|---|---|---|
top / k | char / int | Top letter; k = top.charCodeAt(0) - "A".charCodeAt(0) (4 for E). Rows = k+1. |
| Printed output | text | Symmetric layers of width 2k+1 with a dropping floor. |
k = top.charCodeAt(0) - "A".charCodeAt(0)
for i from k down to 0: // row floor
line = ""
for j from k down to 0: // left half
line += (j > i ? letter[j] : letter[i]) + " "
for j from 1 to k: // right half (skip 0)
line += (j > i ? letter[j] : letter[i]) + " "
console.log(line) | Approach | Idea | Best for |
|---|---|---|
| Two mirrored scans | Left k..0 + right 1..k with j>i | Matching this classic sample |
| Distance from center | append letter by max(dx, dy) style | Concentric / diamond variants |
| Goal | Pattern |
|---|---|
| Alphabet + k | const alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const k = 4; |
| Rows | for (let i = k; i >= 0; i--) |
| Left half | for (let j = k; j >= 0; j--) line += (j > i ? alpha[j] : alpha[i]) + " " |
| Right half | for (let j = 1; j <= k; j++) line += (j > i ? alpha[j] : alpha[i]) + " " |
| Full diamond next | See Program 29 |
Same row - four roles that build the layered square.
leftDescending half through the center A
rightAscending mirror; skips duplicating A
floorBorder letter vs row-floor letter
breakEnds the row after both halves
Reach for this when teaching mirrored scans and shared cell rules for layered squares.
Step up from prefixes to concentric-style layers.
Practice one rule reused on both halves.
Same row logic, then mirror upward for a full diamond.
Map letters to array indexes and reuse them safely.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one shared j > i rule on mirrored halves is the cleanest way to build layered alphabet squares without special-casing the center.
Choose a top letter from A to F and draw the symmetric decreasing alphabet square in the browser.
Three complete JavaScript programs - fixed A–E, user-chosen top letter, and a helper-function rewrite. Click View Output to reveal sample console results, or Try it Yourself to run in the browser editor.
Print five layered rows from E down to the A-center floor.
Two symmetric scans per row with the same j > i check, matching the reference logic.
const k = "E".charCodeAt(0) - "A".charCodeAt(0);
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);
} When i = 2 (floor C), columns where j > 2 append E/D borders, and interior cells append C. The right half starts at j = 1 so the center A is not duplicated on the last row.
Let the user pick the top letter (like E).
Works for A..top with the same symmetric square. Use prompt().trim().toUpperCase() and validate a single A–Z character in real apps.
let top = prompt("Enter top letter (like E):");
top = (top || "").trim().toUpperCase();
if (top.length !== 1 || !/^[A-Z]$/.test(top)) {
console.log("Please enter a single letter.");
} else {
const k = top.charCodeAt(0) - "A".charCodeAt(0);
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);
}
} k = top.charCodeAt(0) - "A".charCodeAt(0) scales both the floor loop and the two halves. Width becomes 2k + 1 (5 letters for top = C).
Same shape with a shared cell helper for both halves.
Often clearer to read: one function applies the floor rule so left and right loops stay thin.
function cell(alpha, j, i) {
return (j > i ? alpha[j] : alpha[i]) + " ";
}
const k = "E".charCodeAt(0) - "A".charCodeAt(0);
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);
} cell owns the j > i rule once. Left and right loops only decide which columns to visit.
We store letters in alpha. For A–E, we set k = 4 (index of E). Every printed cell is one of alpha[0..k].
Row index i runs from k down to 0 (E to A). Smaller i means a deeper inner layer. Think of alpha[i] as the minimum letter allowed in that row.
For columns j = k..0, choose with j > i ? alpha[j] : alpha[i]. Borders stay high; interiors drop to the row floor.
Scan j = 1..k. Starting at 1 (B) avoids appending the center A twice. Total columns: (k+1) + k = 2k + 1 (9 for A–E).
Each row is a symmetric layer around the center. As i decreases, the minimum letter moves inward (E → D → C → B → A) — O(n²) time.
Trace each row floor and the resulting 9-letter line.
i | Floor letter | Printed row |
|---|---|---|
4 | E | E E E E E E E E E |
3 | D | E D D D D D D D E |
2 | C | E D C C C C C D E |
1 | B | E D C B B B C D E |
0 | A | E D C B A B C D E |
Width is always 2×4+1 = 9. The last row is the full palindrome around a single A.
Where this layered alphabet square shows up beyond the homework prompt.
Clearest demo of borders staying high while interiors drop.
Example: flip j > i to j >= i and watch layers shift.
Reuse one cell rule on left and right scans.
Example: start the right half at 0 and see a double A.
Practice k = top.charCodeAt(0) - "A".charCodeAt(0) with an alphabet array.
Example: scale from E to H without rewriting loops.
Factor the floor rule into one method (Example 3).
Example: reuse cell for Program 29 later.
Fixed width × n rows makes O(n²) easy to see.
Example: 5 rows × 9 cells = 45 prints.
Reuse this row logic, then mirror upward for a full diamond.
Example: continue to Program 29.
Pro Tip: say “left E..A, right B..E, print max of column and floor” before coding - that story prevents a duplicated center A.
Why this pattern earns a spot after simpler pyramids and rotations.
A broken mirror or wrong floor shows up immediately.
Both halves reuse the same j > i choice.
Change k and the whole square grows.
The same row logic becomes half of a full diamond.
Pro Tip: learn the inline ternary version first; extract cell once the floor rule feels automatic.
Small habits that keep layered alphabet squares clean.
Starting at 0 duplicates the center A.
Reuse j > i ? alpha[j] : alpha[i] on both halves.
Use k = top.charCodeAt(0) - "A".charCodeAt(0) so scaling stays automatic.
Require a single A–Z character; normalize case if needed.
The sample prints a space after every letter; trim if you need clean ends.
Pro Tip: if the last row shows ... A A B ..., the right half almost certainly started at j = 0.
Mistakes that commonly break symmetric decreasing alphabet squares.
Duplicates the center A.
→ Start the right scan at j = 1.
Using j >= i or swapping operands changes layer borders.
→ Keep j > i ? alpha[j] : alpha[i].
Hard-coding k=4 while changing the alphabet source breaks indexes.
→ Derive k from the chosen top letter.
Empty lines or multi-character input break letter logic or use only the first char.
→ Validate a single A–Z letter after trim().toUpperCase().
Running i from 0 to k prints layers in reverse order.
→ Descend i from k down to 0.
Check these inputs before calling the solution done.
Output is just A (right half empty).
5 rows × width 9 through the A center.
3 rows × width 5 (Example 2).
Normalize with .upper() if needed.
Validate before calling charCodeAt(0).
Replace alpha with 5..1 style indexes.
Try these variations to lock in the pattern.
j > i ? alpha[j] : alpha[i] builds borders and interiors together.2k + 1 (9 for A..E).Quick Takeaway: drop the floor from top to A, print left then right with the same j > i rule, and skip duplicating the center.
| Program | Time | Extra space |
|---|---|---|
| Inline / input (Examples 1–2) | O(n²) | O(1) (plus alphabet source) |
| Helper function (Example 3) | O(n²) | O(1) |
For n letters there are n rows and each row prints O(n) cells (width 2n-1), so total work is O(n²).
The symmetric decreasing alphabet square is a small nested-loop exercise with lasting payoff: mirrored halves, a shared floor rule, and a single center A. Master the classic E…A sample, then try user input and the helper rewrite.
Practice the three examples above, then continue to Program 29’s reverse centered alphabet pyramid.
Drop the floor, print left then right with j > i, start the right half at 1, then break the line.
j = 1j > i floor rule on both halvesk from the top letterconsole.log(line) inside either half loopPrint the symmetric decreasing alphabet square the beginner-friendly way.
Mirror + floor
Definitionj > i ? j : i
CodeStart at B
CodeEnds each row
I/OO(n²) time
AnalysisFix k at the top letter (E). Outer loop i goes from k down to 0 (A). Left half scans j = k..0; right half scans j = 1..k so A appears once in the middle. Each position appends alpha[j] when j > i, otherwise appends alpha[i].
Next up: reverse centered alphabet pyramids that reuse this row logic both downward and upward.
12 people found this page helpful