Shape Rule
Full diamond
Top half: row i prints digit i on mirror diagonals. Bottom half: same logic with i counting down.

Program 58 prints a diagonal mirror number diamond: the top half grows from 1 to rows like Program 57, then a second outer loop mirrors back down to 1. This tutorial covers top/bottom halves, conditional diagonal printing, a live preview, worked JavaScript examples, edge cases, and complexity.
Full diamond
Top half: row i prints digit i on mirror diagonals. Bottom half: same logic with i counting down.
i = 1..rows
for (let i = 1; i <= rows; i++) — same pyramid half as Program 57.
i = rows-1..1
for (let i = rows - 1; i >= 1; i--) — mirrors the top half without repeating the peak row.
j = rows..1
line += (i === j) ? i : " " — reused in both outer loops.
k = 2..rows
line += (i === k) ? i : " " — mirrors the left half from column 2 onward.
Complexity
2×rows-1 lines, each scanning about 2×rows-1 positions — total work grows as O(n²).
A diagonal mirror number diamond extends Program 57’s pyramid: print the top half from 1 to rows, then mirror back down with a second outer loop from rows-1 to 1. With rows = 5, you get nine lines — peak at row 5, then symmetric descent to a single 1.
Each row reuses Program 57’s inner loops: left diagonal j = rows..1, right diagonal k = 2..rows, appending the digit only when i === j or i === k.
It bridges Program 57’s single pyramid to full symmetry — one extra outer loop turns a half-pattern into a complete diamond.
i = 1..rows — pyramid grows upward.
i = rows-1..1 — mirror without repeating peak.
Program 57 is the top half only; Program 58 adds the mirrored bottom loop.
Follow Program 57; continue to Program 59 next.
In short: top loop 1..rows, bottom loop rows-1..1, same inner diagonal logic per row, then console.log(line).
Given row count rows = 5, print a diagonal mirror number diamond — top half 1..rows, bottom half rows-1..1, with mirror diagonals on every line.
// rows = 5
// 1
// 2 2
// 3 3
// 4 4
// 5 5
// 4 4
// 3 3
// 2 2
// 1 | Item | Type | Description |
|---|---|---|
rows | int | Half-height — diamond has 2×rows-1 total lines. |
i (top outer) | int | Runs 1 to rows — builds the upper half. |
i (bottom outer) | int | Runs rows-1 down to 1 — mirrors without repeating peak. |
j (left) | int | Scans rows..1 — appends digit when i === j. |
k (right) | int | Scans 2..rows — prints digit when i == k. |
| Total lines | int | rows + (rows - 1) = 2×rows - 1. |
for i from 1 to rows:
print row i with left and right diagonal logic
for i from rows - 1 down to 1:
print row i with same inner loop logic | Approach | Idea | Best for |
|---|---|---|
| Two outer loops | Top 1..rows, bottom rows-1..1 | Learning and interviews |
| Reuse inner logic | Same j and k loops in both halves | DRY diamond patterns |
| User-input rows | prompt() + parseInt() | Flexible diamond size |
| Compact trace | rows = 3 on paper first | Quick dry-runs before full demo |
| Character swap | Replace digit with * for an X-diamond | Visual debugging |
| Goal | Pattern |
|---|---|
| Top outer loop | for (let i = 1; i <= rows; i++) |
| Bottom outer loop | for (let i = rows - 1; i >= 1; i--) |
| Left diagonal | for (let j = rows; j >= 1; j--) line += (i === j) ? i : " " |
| Right diagonal | for (let k = 2; k <= rows; k++) line += (i === k) ? i : " " |
| End row | console.log(line) |
| Program 57 contrast | Program 57 is top half only; Program 58 adds bottom mirror loop |
Same diagonal mirror diamond — three ways to set row count and trace the logic.
rows = 5Hard-coded half-height for demos
parseInt(prompt())Read row count from console
rows = 35-line diamond dry-run
i = 1..rowsProgram 57 pyramid logic
i = rows-1..1Mirror without peak repeat
Reach for this pattern when teaching symmetry, mirroring loops, and extending a half-pattern into a full diamond.
Natural follow-up after Program 57’s pyramid — one extra outer loop completes the diamond.
Top and bottom halves share inner logic — good bridge to palindrome and mirror problems.
Separate top and bottom boundaries — concrete loop-boundary practice.
Compare this hollow 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 extension that locks in mirroring, symmetry, and O(n²) thinking.
Choose row count between 3 and 9 and draw the centered diagonal mirror number 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 diagonal mirror diamond with half-height five — top loop 1..rows, bottom loop rows-1..1.
rows = 5Hard-coded half-height — log the top pyramid, then mirror with a second outer loop using the same inner diagonal logic.
const rows = 5;
for (let i = 1; i <= rows; i++) {
let line = "";
for (let j = rows; j >= 1; j--) {
line += (i === j) ? i : " ";
}
for (let k = 2; k <= rows; k++) {
line += (i === k) ? i : " ";
}
console.log(line);
}
for (let i = rows - 1; i >= 1; i--) {
let line = "";
for (let j = rows; j >= 1; j--) {
line += (i === j) ? i : " ";
}
for (let k = 2; k <= rows; k++) {
line += (i === k) ? i : " ";
}
console.log(line);
} The first outer loop prints rows 1 through 5 (Program 57 logic). The second outer loop prints rows 4 down to 1 — reusing the same inner loops so the bottom half mirrors the top without repeating row 5.
Read half-height 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 = rows; j >= 1; j--) {
line += (i === j) ? i : " ";
}
for (let k = 2; k <= rows; k++) {
line += (i === k) ? i : " ";
}
console.log(line);
}
for (let i = rows - 1; i >= 1; i--) {
let line = "";
for (let j = rows; j >= 1; j--) {
line += (i === j) ? i : " ";
}
for (let k = 2; k <= rows; k++) {
line += (i === k) ? i : " ";
}
console.log(line);
}
} Same top-and-bottom outer loops as Example 1; only the source of rows changes from a literal to user input.
Smaller half-height for quick tracing on paper or in interviews.
rows = 3Use rows = 3 for a 5-line diamond — trace both outer loops before scaling to 5.
const rows = 3;
for (let i = 1; i <= rows; i++) {
let line = "";
for (let j = rows; j >= 1; j--) {
line += (i === j) ? i : " ";
}
for (let k = 2; k <= rows; k++) {
line += (i === k) ? i : " ";
}
console.log(line);
}
for (let i = rows - 1; i >= 1; i--) {
let line = "";
for (let j = rows; j >= 1; j--) {
line += (i === j) ? i : " ";
}
for (let k = 2; k <= rows; k++) {
line += (i === k) ? i : " ";
}
console.log(line);
} With half-height 3 you get 5 total lines — enough to trace top loop, peak row, and bottom mirror on paper before the full demo.
rows = 5 is half-height — the diamond prints 2×rows-1 = 9 lines.
for (let i = 1; i <= rows; i++) — Program 57 pyramid logic with left and right diagonal inner loops.
for (let i = rows - 1; i >= 1; i--) — same inner loops, counting down to avoid repeating the peak row.
Left j = rows..1, right k = 2..rows — append digit when i === j or i === k, else space.
2×rows-1 lines total — O(n²) time, O(1) extra memory.
rows = 5Trace each line’s half (top or bottom), row index, and diagonal hits — nine lines total.
| Line | Half | i | Left hit | Right hit |
|---|---|---|---|---|
| 1 | Top | 1 | j=1 | (none) |
| 2 | Top | 2 | j=2 | k=2 |
| 3 | Top | 3 | j=3 | k=3 |
| 4 | Top | 4 | j=4 | k=4 |
| 5 | Top (peak) | 5 | j=5 | k=5 |
| 6 | Bottom | 4 | j=4 | k=4 |
| 7 | Bottom | 3 | j=3 | k=3 |
| 8 | Bottom | 2 | j=2 | k=2 |
| 9 | Bottom | 1 | j=1 | (none) |
The bottom loop starts at rows-1 so line 5 (peak) is not printed twice — total lines = 2×rows-1.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Two inner loops with conditional printing — classic diagonal placement drill.
Example: trace each row in the walkthrough table — left hit, right hit.
Each row mirrors digits on two diagonals — compare with Program 53’s single diagonal V-shape.
Example: row 5 prints 5 at column 5 and again at column 9.
Practice building one line string per row with digit-or-space decisions per column.
Example: call console.log() inside the inner loop by mistake.
Starting the right loop at 2 avoids a third digit at the center — keeps exactly two prints per row.
Example: try k = 1 and see the center digit triple on some rows.
Each row scans about 2n positions — makes O(n²) concrete for beginners.
Example: row 5 with rows = 5 scans 9 character slots — see the walkthrough table.
Pair the pattern with Number.isFinite and positive-row validation 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.
The full diamond is instantly recognizable — top half grows, bottom half mirrors symmetrically.
Conditional digit-or-space printing teaches real console alignment — not abstract loop drill.
Swap digits for * to get an X-diamond, or try fixed-width formatting for rows beyond 9.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace rows = 3 on paper — 5 lines total, peak at row 3, then mirror back to 1.
Small habits that keep number-pattern code clean.
Append the digit when i === j; otherwise append a single space.
Avoid crashing when the user types letters instead of a number.
Only call console.log(line) after both inner loops finish the row.
Use for (let k = 2; k <= rows; k++) so the center position is not duplicated.
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 called console.log() inside the inner loop.
Mistakes that commonly break diagonal mirror number diamond patterns.
Each digit lands on its own line — you get a column, not a pyramid.
→ Use line += (i === j) ? i : " "; call console.log(line) only after both inner loops.
Starting at k = 1 can print a third digit at the center — row looks crowded.
→ Use for (let k = 2; k <= rows; k++) — mirror from column 2 onward.
Using j === rows instead of i === j places digits on the wrong diagonal.
→ Always compare the outer row index i with the inner loop variable j or k.
All numbers print on one long line without row breaks.
→ Call console.log(line) after both inner loops complete.
Letters or empty input yield NaN or leave rows invalid.
→ Validate with Number.isFinite(rows) and check rows >= 1 before drawing.
Check these inputs before calling the solution done.
Output is just 1 — the right loop (k = 2..1) does not run.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Bottom row has two copies of 5 across 9 positions — good for dry-runs before scaling up.
Bare parseInt(prompt()) returns NaN — validate with Number.isFinite.
Row 9 scans 17 character positions (2×9-1) — total work grows as O(n²).
Try these variations to lock in the pattern.
rows-1..1 for the full diamondrows and see the middle row print twicerows-1"*" in both diagonal branchesi = 1..rows. Bottom: i = rows-1..1. Same inner diagonal logic in both.line string per row with += — call console.log(line) only after both inner loops finish.rows > 0 for interactive programs; rows = 1 prints one line — bottom loop does not run.2×rows-1 lines, each scanning about 2×rows-1 positions — total work grows as O(n²).Quick Takeaway: top 1..rows, bottom rows-1..1, same inner diagonal logic, then console.log(line).
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Digits on row i | 2i - 1 | No storage beyond loop counters |
The diagonal mirror number diamond is a natural follow-up to Program 57: one extra outer loop mirrors the pyramid 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 59 for the next pattern in the series.
Total output is 2×rows-1 lines — peak at row rows, then mirrored descent to 1.
for (let j = rows; j >= 1; j--) with if (i === j)for (let k = 2; k <= rows; k++) with if (i === k)console.log(line) after both inner loopsNumber.isFinite after parseInt(prompt()) for user inputk = 1 — can triple-print at centerj === rows instead of i === j — wrong diagonalconsole.log() inside any inner looprows = 3 dry-run before coding rows = 5Print the full mirror-diagonal diamond the beginner-friendly way.
2×rows-1 lines
Definitionj = rows..1
Codek = 2..rows
Codei === j or i === k
LogicO(n²) time
AnalysisPrint the Program 57 pyramid for the top half, then mirror with for (let i = rows - 1; i >= 1; i--). Total lines = 2×rows-1 — each row scans about 2×rows-1 positions.
Move on to the next pattern in the JavaScript number-pattern series.
12 people found this page helpful