Shape Rule
Full diamond
Top half grows from 1 to rows; bottom half mirrors from rows-1 back to 1 — total 2n-1 lines.

Program 54 prints a mirror diagonal diamond pattern: the top half matches Program 53’s V-shape, then a second outer loop mirrors it downward to form a full diamond — a natural step after Program 53’s mirror diagonal pattern. This tutorial covers two outer loops, i === j and i === k conditions, a live preview, worked JavaScript examples, edge cases, and complexity.
Full diamond
Top half grows from 1 to rows; bottom half mirrors from rows-1 back to 1 — total 2n-1 lines.
i = 1..rows
for (let i = 1; i <= rows; i++) logs the upper V-half — same logic as Program 53.
i = rows-1..1
for (let i = rows - 1; i >= 1; i--) mirrors the top half without duplicating the peak row.
i === j
line += (i === j ? i : " ") — digit on the main diagonal each row.
i === k
line += (i === k ? i : " ") — mirrored diagonal; right loop starts at rows-1.
Complexity
About 2n-1 lines, each scanning 2n-1 positions — total work grows as O(n²).
A mirror diagonal diamond pattern extends Program 53’s V-shape: print the top half from 1 to rows, then mirror the same row logic from rows-1 back to 1. With rows = 5, you get nine lines ending with 1 1 at the bottom.
In JavaScript, use two outer loops — top and bottom — each with left (i === j) and right (i === k) inner loops, appending spaces elsewhere before console.log(line).
It bridges Program 53’s single V-half to full symmetry — teaching how to mirror loop ranges without duplicating the peak row.
i = 1..rows — same as Program 53.
i = rows-1..1 — mirrors without duplicating the peak.
Program 53 stops at the V tip; Program 54 adds a second outer loop to complete the diamond.
Follow Program 53; continue to Program 55 next.
In short: top loop for (let i = 1; i <= rows; i++), bottom loop for (let i = rows - 1; i >= 1; i--), each row uses i === j and i === k, then console.log(line).
Given row count rows = 5, print a mirror diagonal diamond — top half grows to rows, bottom half mirrors back to 1.
// rows = 5
// 1 1
// 2 2
// 3 3
// 4 4
// 5
// 4 4
// 3 3
// 2 2
// 1 1 | Item | Type | Description |
|---|---|---|
rows | number | Peak row of the diamond (total lines = 2n-1). |
i (top outer) | number | Runs 1 to rows for the upper half. |
i (bottom outer) | number | Runs rows-1 down to 1 for the lower half. |
j (left) | number | Scans 1..rows; appends digit when i === j. |
k (right) | number | Scans rows-1..1; appends digit when i === k. |
| Total lines | number | 2 * rows - 1 lines for a complete diamond. |
for i from 1 to rows:
line = ""
append digit when i === j or space (left loop)
append digit when i === k or space (right loop)
console.log(line)
for i from rows - 1 down to 1:
same row logic (mirror bottom half) | Approach | Idea | Best for |
|---|---|---|
| Two outer loops | Top 1..rows, bottom rows-1..1 | Learning symmetry and loop bounds |
| Reuse row logic | Same inner loops in both outer loops | DRY diamond construction |
| User-input rows | parseInt(prompt()) | Flexible diamond size |
| Compact trace | rows = 3 on paper first | Quick dry-runs (5 lines total) |
| Extract row method | PrintRow(i, rows) called twice | Cleaner code after mastering loops |
| Goal | Pattern |
|---|---|
| Top outer loop | for (let i = 1; i <= rows; i++) |
| Bottom outer loop | for (let i = rows - 1; i >= 1; i--) |
| Left half | for (let j = 1; j <= rows; j++) line += (i === j ? i : " "); |
| Right half | for (let k = rows - 1; k >= 1; k--) line += (i === k ? i : " "); |
| End row | console.log(line); |
| Program 53 contrast | Program 53 prints top V-half only; Program 54 adds bottom mirror loop |
Same diamond — three ways to set row count and trace the symmetry.
rows = 5Hard-coded peak for demos (9 lines)
parseInt(prompt())Read peak row from console
rows = 35-line diamond for paper tracing
i = 1..rowsUpper V — same as Program 53
i = rows-1..1Mirror without duplicating peak
Reach for this pattern when teaching symmetry, mirrored loop ranges, and completing a V-shape into a diamond.
Natural follow-up after Program 53’s V-half — adds the bottom mirror loop to complete the diamond.
Teaches why the bottom loop starts at rows-1 — a pattern used in many diamond and pyramid programs.
About 2n-1 lines, each scanning 2n-1 positions — concrete O(n²) complexity.
Compare this number diamond with the next pattern in the series.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in symmetry, mirrored loop bounds, and O(n²) thinking.
Choose peak row count between 3 and 9 and draw the full mirror diagonal diamond in the browser.
Three complete JavaScript programs — fixed rows, user input, and a compact trace demo. Click View Output to reveal sample console results, or Try it Yourself to run in the browser.
Print a full mirror diagonal diamond with peak row 5 — top V-half plus mirrored bottom half.
rows = 5Hard-coded peak row — top loop 1..rows, bottom loop rows-1..1, same inner diagonal logic each row.
const rows = 5;
for (let i = 1; i <= rows; i++) {
let line = "";
for (let j = 1; j <= rows; j++) {
line += (i === j ? i : " ");
}
for (let k = rows - 1; k >= 1; k--) {
line += (i === k ? i : " ");
}
console.log(line);
}
for (let i = rows - 1; i >= 1; i--) {
let line = "";
for (let j = 1; j <= rows; j++) {
line += (i === j ? i : " ");
}
for (let k = rows - 1; k >= 1; k--) {
line += (i === k ? i : " ");
}
console.log(line);
} The first outer loop logs rows 1 through 5 (Program 53’s V-half). The second outer loop logs rows 4 down to 1, reusing the same inner loops — nine lines total without duplicating row 5.
Read peak row count with prompt() and Number.isFinite validation.
Read rows with prompt() and validate the result.
const rowsInput = prompt("Enter the number of rows:");
const rows = parseInt(rowsInput, 10);
if (!Number.isFinite(rows) || rows < 1) {
console.log("Please enter a positive integer.");
} else {
for (let i = 1; i <= rows; i++) {
let line = "";
for (let j = 1; j <= rows; j++) {
line += (i === j ? i : " ");
}
for (let k = rows - 1; k >= 1; k--) {
line += (i === k ? i : " ");
}
console.log(line);
}
for (let i = rows - 1; i >= 1; i--) {
let line = "";
for (let j = 1; j <= rows; j++) {
line += (i === j ? i : " ");
}
for (let k = rows - 1; k >= 1; k--) {
line += (i === k ? i : " ");
}
console.log(line);
}
} Same two-outer-loop diamond core as Example 1; only the source of rows changes from a literal to user input.
Smaller peak row for quick tracing on paper or in interviews.
rows = 3Use rows = 3 to trace top loop, bottom loop, and symmetry before scaling to 5 rows.
const rows = 3;
for (let i = 1; i <= rows; i++) {
let line = "";
for (let j = 1; j <= rows; j++) {
line += (i === j ? i : " ");
}
for (let k = rows - 1; k >= 1; k--) {
line += (i === k ? i : " ");
}
console.log(line);
}
for (let i = rows - 1; i >= 1; i--) {
let line = "";
for (let j = 1; j <= rows; j++) {
line += (i === j ? i : " ");
}
for (let k = rows - 1; k >= 1; k--) {
line += (i === k ? i : " ");
}
console.log(line);
} Five lines total — top 3 rows plus bottom 2 — let you trace both outer loops on paper before running the full rows = 5 demo.
const rows = 5; — the diamond will have 2*rows-1 = 9 lines.
for (let i = 1; i <= rows; i++) — same V-half logic as Program 53.
Left loop i === j, right loop i === k — spaces fill all other columns.
for (let i = rows - 1; i >= 1; i--) reuses the same inner loops — skips the peak row.
2n-1 lines, each about 2n-1 characters — O(n²) time, O(1) extra memory.
rows = 5Trace each line’s outer-loop phase, diagonal positions, and full output.
| Line | Phase | i | Row output |
|---|---|---|---|
| 1 | Top | 1 | 1 1 |
| 2 | Top | 2 | 2 2 |
| 3 | Top | 3 | 3 3 |
| 4 | Top | 4 | 4 4 |
| 5 | Top (peak) | 5 | 5 |
| 6 | Bottom | 4 | 4 4 |
| 7 | Bottom | 3 | 3 3 |
| 8 | Bottom | 2 | 2 2 |
| 9 | Bottom | 1 | 1 1 |
The bottom loop starts at i = rows - 1 so line 5 (the peak) is not printed twice.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Two outer loops mirror the same row logic — top then bottom.
Example: trace lines 1–9 in the walkthrough table.
The bottom loop starting at rows-1 is a classic symmetry trick used in many diamond patterns.
Example: line 5 is the peak; lines 6–9 mirror lines 4–1.
Practice line += (i === j ? i : " ") vs console.log(line) with two inner loops per row.
Example: put console.log(line) inside the inner loop by mistake.
Total lines = 2n-1 — links symmetry to loop-bound formulas.
Example: Peak row 10 produces 19 lines total.
Growing inner bound makes O(n²) concrete — count prints for n rows.
Example: Peak row 5 produces 9 lines — see the walkthrough table.
Pair the pattern with Number.isFinite and positive-row checks after parseInt(prompt()).
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner JavaScript courses.
Starting the bottom loop at rows duplicates the peak row immediately.
Two outer loops teach real symmetry — not abstract loop drill.
Change rows, use fixed-width format, or switch to full rectangular table.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace rows = 3 on paper — 5 lines total, peak at line 3.
Small habits that keep number-pattern code clean.
Scan all columns in the left half — append digit only when i === j.
Number.isFiniteAvoid crashes when the user types letters instead of a number.
Only call console.log(line) after both inner loops finish the row.
Start the right loop at rows - 1 to skip duplicating the center column.
Trace five rows on paper before coding the full 10-row demo.
Pro Tip: if the output is a vertical list of single numbers, you almost certainly put console.log(line) inside the inner loop.
Mistakes that commonly break mirror diagonal diamond patterns.
Each character lands on its own line — you get a column, not a diamond.
→ Use line += (i === j ? i : " ") in both loops; console.log(line) only after both inner loops.
Starting the bottom outer loop at i = rows prints the peak row twice.
→ Use for (let i = rows - 1; i >= 1; i--) for the bottom half.
The middle row of the diamond appears twice — breaking symmetry.
→ Start the bottom outer loop at rows - 1, not rows.
All numbers log on one long line without row breaks.
→ Add console.log(line) after both inner loops complete.
Letters or empty input return NaN when parseInt(prompt()) is unchecked.
→ Validate with Number.isFinite(rows) and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 — the right loop does not run.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Peak row 5 produces 9 lines — good for dry-runs.
Unchecked parseInt(prompt()) returns NaN — use Number.isFinite.
Row 9 scans 17 character positions — total work grows as O(n²).
Try these variations to lock in the pattern.
2n-1PrintRow(i, rows)i = 1..rows. Bottom: i = rows-1..1. Same inner loops in both.line += ... builds the row; console.log(line) advances — call it after both inner loops finish each row.rows > 0 for interactive programs; total output lines = 2*rows - 1.2n-1 lines, each scanning 2n-1 positions — total work is O(n²).Quick Takeaway: top loop for (let i = 1; i <= rows; i++), bottom loop for (let i = rows - 1; i >= 1; i--), each row uses i === j and i === k, then console.log(line).
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Total lines | 2n - 1 | About 2n-1 chars per line |
The mirror diagonal diamond pattern is a natural follow-up to Program 53: add a second outer loop to mirror the V-half downward into a full symmetric diamond. Master the fixed-rows version, then try user input and the compact 3-row trace.
Practice the three examples above, then continue to Program 55 for the next pattern in the series.
Total lines = 2n-1 — bottom loop starts at rows-1 to avoid duplicating the peak.
for (let i = 1; i <= rows; i++)for (let i = rows - 1; i >= 1; i--)console.log(line) after both inner loops each rowNumber.isFinite for user inputi = rows — duplicates the peak rowconsole.log(line) inside either inner looprows = 3 dry-run before coding rows = 5Print the full diamond the beginner-friendly way.
Top V + mirrored bottom
Definitioni = 1..rows
Codei = rows-1..1
Code2n - 1 lines
LogicO(n²) time
AnalysisProgram 53’s V-shape becomes a full diamond by adding a second outer loop from rows-1 down to 1. Total lines = 2n-1 with about 2n-1 characters per line — O(n²) overall.
Move on to the next pattern in the JavaScript number-pattern series.
12 people found this page helpful