Shape Rule
Reverse + diagonal
Each row is top..A with one cell replaced by *.

Print the reverse alphabet line EDCBA on every row, but replace one character per row with * where i === j. The star slides from right to left: EDCB*, EDC*A, ED*BA, E*CBA, *DCBA. Includes a live preview, worked JavaScript examples, edge cases, and complexity. Next: Program 18 (palindromic pyramid).
Reverse + diagonal
Each row is top..A with one cell replaced by *.
Row key
for (let i = base; i <= top; i++) picks which letter becomes *.
E down to A
j walks reverse letters; print * when i === j.
i === j
One match per row — the star slides left as i grows.
Top letter
Pick a top letter A–Z and draw the grid instantly.
Complexity
An n×n grid prints n characters per row.
A reverse alphabet diagonal-star pattern prints the same reverse letter run on every row (for example EDCBA), then swaps exactly one cell for * where the row key equals the column letter.
In JavaScript you solve it with nested ord/chr loops: outer i walks A..top, inner j walks top..A, and a simple i === j test decides star vs letter.
It teaches diagonal thinking on a character grid — the same i === j idea used in matrix diagonals, using letter codes from charCodeAt.
Inner loop prints top down to A.
i === j picks one star per row.
As i grows, the match moves left.
n letters ⇒ n rows × n columns.
In short: for each row key i, append top..A, but append * wherever j equals i, then call console.log(line).
Given a top letter (like E), print an n×n grid of reverse letters with one diagonal star per row.
# Classic sample (top = E)
# EDCB*
# EDC*A
# ED*BA
# E*CBA
# *DCBA | Item | Type | Description |
|---|---|---|
top | char | Highest letter (e.g. E). Grid size n = top − ‘A’ + 1. |
| Printed output | text | n rows of reverse letters with one * on the i === j diagonal. |
for i from base to top:
line = ""
for j from top down to base:
line += (i === j ? "*" : String.fromCharCode(j))
console.log(line) | Approach | Idea | Best for |
|---|---|---|
Char loops + i === j | Compare letters directly | Matching this classic sample |
| Index loops | Rows/cols 0..n-1, map to letters | When you already think in matrix indices |
| Goal | Pattern |
|---|---|
| Row keys | for (let i = base; i <= top; i++) |
| Reverse columns | for (let j = top; j >= base; j--) |
| Diagonal star | line += (i === j ? "*" : String.fromCharCode(j)) |
| End the row | console.log(line) |
| Forward letters | Inner loop j = 'A'..top (Example 3) |
| Other marker | Swap '*' for '#', '@', etc. |
Same grid — different roles on each inner-loop pass.
i === jDiagonal cell for the current row key
elseReverse letter when not on the diagonal
E..AInner direction makes the reverse run
breakEnds the row after n columns
Reach for this when teaching diagonals on a character grid.
You already print E..A; now mark one cell per row.
Practice i === j with letter codes from charCodeAt.
See rows and columns as a full rectangle of letters.
Replace letters with markers — useful for puzzle-style labs.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one tiny condition (i === j) turns a flat reverse grid into a moving diagonal.
Enter a top letter from A to Z and draw the reverse diagonal-star grid in the browser.
Three complete JavaScript programs - fixed top E, user-chosen top letter, and a forward A..E diagonal variant. Click View Output to reveal sample console results, or Try it Yourself to run in the browser editor.
Print the classic 5×5 reverse grid with a sliding star.
EOuter i runs A..E. Inner j runs E..A. When i === j, append *; otherwise append j.
const base = "A".charCodeAt(0);
const top = "E".charCodeAt(0);
for (let i = base; i <= top; i++) {
let line = "";
for (let j = top; j >= base; j--) {
if (i === j) {
line += "*";
} else {
line += String.fromCharCode(j);
}
}
console.log(line);
} Row i = "A".charCodeAt(0) matches when j reaches A (rightmost column) → EDCB*. Row i = "B".charCodeAt(0) matches one column earlier → EDC*A. By i = "E".charCodeAt(0), the star is at the leftmost column → *DCBA.
Let the user choose the top letter.
Read the top letter (like E or D) and generate the same pattern for A..top. Prefer validating a single A–Z character from prompt().trim().toUpperCase() in real apps.
const raw = (prompt("Enter the top letter (like E):") || "").trim().toUpperCase();
const top = raw ? raw.charCodeAt(0) : "E".charCodeAt(0);
const base = "A".charCodeAt(0);
for (let i = base; i <= top; i++) {
let line = "";
for (let j = top; j >= base; j--) {
line += (i === j) ? "*" : String.fromCharCode(j);
}
console.log(line);
} Same i === j diagonal; only the bounds follow top. With top = "D".charCodeAt(0) you get a 4×4 grid and the star still slides right → left.
Same diagonal test with forward letters.
Flip the inner loop to A..E. The star now slides left → right on the main diagonal.
const base = "A".charCodeAt(0);
const top = "E".charCodeAt(0);
for (let i = base; i <= top; i++) {
let line = "";
for (let j = base; j <= top; j++) {
line += (i === j) ? "*" : String.fromCharCode(j);
}
console.log(line);
} The diagonal rule is unchanged - only column order flips. Comparing Examples 1 and 3 shows how inner-loop direction controls both letter order and which way the star travels.
Use prompt() when reading input. Choose a top letter (fixed or input).
i takes A, B, C… top — the letter that becomes * on that row.
j runs from top down to A. Default cells print j; the match prints *.
console.log(line) ends the row so the next key starts a fresh line.
n letters ⇒ n×n cells — O(n²) time, O(1) extra memory.
ETrace each outer value of i and see which column becomes the star.
i | Columns (E..A) | Where i === j | Printed row |
|---|---|---|---|
A | E D C B A | last column (A) | EDCB* |
B | E D C B A | 4th column (B) | EDC*A |
C | E D C B A | 3rd column (C) | ED*BA |
D | E D C B A | 2nd column (D) | E*CBA |
E | E D C B A | 1st column (E) | *DCBA |
Exactly one star per row; the match walks from right to left as i increases.
Where this diagonal-star idea shows up beyond the homework prompt.
Clearest alphabet demo of i === j on a grid.
Example: print all letters first, then add the star condition.
Flip inner-loop direction to move the star the other way.
Example: compare Examples 1 and 3 side by side.
Swap * for #, @, or a digit.
Example: print row number on the diagonal instead.
Same idea as marking the main diagonal of a matrix.
Example: later rewrite with integer row/col indices.
Full rectangles make O(n²) easy to count.
Example: 5 rows × 5 columns = 25 writes.
Practice reading and validating a single letter input.
Example: reject empty strings and non A–Z input.
Pro Tip: say “print reverse letters, replace the match with a star” before coding — that story prevents wrong loop bounds.
Why this pattern earns a spot after plain reverse letter grids.
Wrong bounds or a missing condition show up as a broken diagonal immediately.
One comparison (i === j) drives the entire special effect.
Forward letters, other markers, and input tops are one-line tweaks.
Streaming output needs no storage beyond loop variables.
Pro Tip: master the reverse version first; treat the forward diagonal as a direction flip afterward.
Small habits that keep diagonal-star code clean.
Outer and inner must share the same letter range or the diagonal will miss.
Reverse (top..A) vs forward (A..top) changes where the star travels.
Require a single A–Z character; empty input breaks top_ch[0].
.toUpperCase() keeps mixed input consistent with "A".charCodeAt(0)..top.
Trace one star position per row on paper before coding larger tops.
Pro Tip: if every row prints a full reverse line with no star, you almost certainly forgot the i === j branch.
Mistakes that commonly break reverse diagonal-star patterns.
Different ranges for i and j mean some rows never hit i === j.
→ Use the same top for both loops.
Going A..E when you wanted E..A prints a different pattern.
→ Decide reverse vs forward before coding (Examples 1 vs 3).
Testing i == 'E' or column index alone breaks the sliding diagonal.
→ Compare the row key to the current column letter: i === j.
top_ch[0]Empty or multi-character input can throw or pick the wrong char.
→ Read a string, check length, take [0], validate A–Z.
Breaks the row into one character per line.
→ Call console.log(line) only after the inner loop finishes.
Check these inputs before calling the solution done.
Output is just * on one line.
Five rows through *DCBA.
Same rule, smaller size (Example 2).
Normalize to upper, or use 'a'..'e' consistently.
top_ch[0] fails on empty tokens — validate first.
Same loops; only the diagonal character changes.
Try these variations to lock in the pattern.
# instead of *i === j matches exactly once while j walks the shared range.Quick Takeaway: walk reverse letters, replace the matching column with *, then break the line — that is the whole pattern.
| Program | Time | Extra space |
|---|---|---|
| Fixed / input reverse diagonal (Examples 1–2) | O(n²) | O(1) |
| Forward diagonal (Example 3) | O(n²) | O(1) |
With n = top − ‘A’ + 1, every row prints n characters, so total work is O(n²).
The reverse alphabet diagonal-star pattern is a small nested-loop exercise with lasting payoff: reverse column order, a shared letter range, and one diagonal test. Master the classic EDCB* sample, then try user input and the forward-letter variant.
Practice the three examples above, then continue to Program 18’s palindromic alphabet pyramid.
Use matching A..top bounds, print reverse letters, swap * when i === j, and break only after the inner loop.
i === j for the diagonal stari and jconsole.log(line) inside the column looptop_ch[0]Print the reverse diagonal-star grid the beginner-friendly way.
Reverse + diagonal *
Definitiontop down to A
Codei === j → *
CodeEnds each row
I/OO(n²) time
AnalysisThe diagonal is defined by i === j while the row prints letters from E down to A. Since i increases A..E each row, the star moves one position left each line: EDCB*, EDC*A, ED*BA, E*CBA, *DCBA.
Next up: palindromic alphabet pyramids (A, ABA, ABCBA, …).
12 people found this page helpful