Shape Rule
Mirror diagonals
Row i prints the digit i on the left diagonal and again on the right diagonal — an inverse-V mirror shape.

Program 57 prints a diagonal mirror number pyramid: each row shows the row number twice — once on the left diagonal and once on the right — with spaces everywhere else. A natural step after Program 56’s centered palindromic pyramid. This tutorial covers conditional printing, two inner loops per row, a live preview, worked JavaScript examples, edge cases, and complexity.
Mirror diagonals
Row i prints the digit i on the left diagonal and again on the right diagonal — an inverse-V mirror shape.
i = 1..rows
for (let i = 1; i <= rows; i++) picks the current row and the digit to place on both diagonals.
j = rows..1
line += (i === j) ? i : " " — scans from the right edge inward.
k = 2..rows
line += (i === k) ? i : " " — mirrors the left half from column 2 onward.
digit or space
Every column position gets either the row digit or a single space — no other characters.
Complexity
Each row scans about 2×rows-1 positions — total work grows as O(n²).
A diagonal mirror number pyramid prints the row number on two mirror diagonals with spaces everywhere else. With rows = 5, you get 1, 2 2, 3 3, and so on — forming an inverse-V shape.
In JavaScript use an outer loop for rows, then two inner loops: the first scans j = rows..1 for the left diagonal, the second scans k = 2..rows for the right diagonal, appending the digit only when i === j or i === k.
It bridges Program 56’s palindromic rows to conditional diagonal placement — combining if checks with two inner loops per row.
j = rows..1, append when i === j.
k = 2..rows, append when i === k.
Program 56 prints palindromic rows; Program 57 prints the row digit twice on mirror diagonals.
Follow Program 56; continue to Program 58 next.
In short: outer i = 1..rows, left loop j = rows..1, right loop k = 2..rows, append digit or space, then console.log(line).
Given row count rows = 5, print a diagonal mirror number pyramid — row i shows digit i on left and right diagonals with spaces elsewhere.
// rows = 5
// 1
// 2 2
// 3 3
// 4 4
//5 5 | Item | Type | Description |
|---|---|---|
rows | int | Pyramid height — bottom row has rows on both diagonals. |
i (outer) | int | Current row index — runs 1 to rows. |
j (left) | int | Scans rows..1 — appends digit when i === j. |
k (right) | int | Scans 2..rows — appends digit when i === k. |
| Positions per row | int | rows + (rows - 1) = 2×rows - 1 character slots. |
| Digits per row | int | Exactly 2 (except row 1 when right loop is empty for rows = 1). |
for i from 1 to rows:
for j from rows down to 1:
append i if i === j else space to line
for k from 2 to rows:
append i if i === k else space to line
console.log(line) | Approach | Idea | Best for |
|---|---|---|
| Two inner loops | Left j = rows..1, right k = 2..rows | Learning and interviews |
| Conditional append | if (i === j) digit else space | Diagonal placement drills |
| User-input rows | prompt() + parseInt() | Flexible pyramid size |
| Compact trace | rows = 3 on paper first | Quick dry-runs before full demo |
| Character swap | Replace digit with * for an X-shape | Visual debugging |
| Goal | Pattern |
|---|---|
| Outer loop | for (let i = 1; i <= rows; 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 56 contrast | Program 56 uses palindromic rows; Program 57 uses mirror diagonals |
Same diagonal mirror pyramid — three ways to set row count and trace the logic.
rows = 5Hard-coded height for demos
parseInt(prompt())Read row count from console
rows = 3Quick dry-run on paper
j = rows..1Append when i === j
k = 2..rowsAppend when i === k
Reach for this pattern when teaching conditional printing, diagonal placement, and combining two inner loops per row.
Natural follow-up after Program 56’s palindromic pyramid — introduces conditional diagonal placement.
Each row places digits on mirror diagonals — good bridge to matrix diagonal problems.
Left and right halves with if checks — concrete nested-loop practice.
Program 58 extends this diagonal logic to a full diamond — compare after mastering this pyramid.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in conditional printing, mirror diagonals, and O(n²) thinking.
Choose row count between 3 and 9 and draw the centered diagonal mirror number pyramid 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 diagonal mirror pyramid with five rows — left and right inner loops with conditional printing per row.
rows = 5Hard-coded row count — scan left diagonal j = rows..1, then right diagonal k = 2..rows, appending digit or space.
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);
} When i = 3, the left loop prints spaces then 3 at j = 3; the right loop prints spaces then 3 at k = 3 — output 3 3. When i = 1, only the left loop places a digit; the right loop is all spaces.
Read 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 = rows; j >= 1; j--) {
line += (i === j) ? i : " ";
}
for (let k = 2; k <= rows; k++) {
line += (i === k) ? i : " ";
}
console.log(line);
}
} Same conditional diagonal logic as Example 1; only the source of rows changes from a literal to user input.
Smaller row count for quick tracing on paper or in interviews.
rows = 3Use rows = 3 to trace left and right diagonal loops before scaling to 5 rows.
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);
} With only three rows you can trace every i === j and i === k check on paper before running the full rows = 5 demo.
const rows = 5; controls pyramid height and the maximum digit printed.
for (let j = rows; j >= 1; j--) — append digit when i === j, else space.
for (let k = 2; k <= rows; k++) — append digit when i === k, else space.
Call console.log(line) after both inner loops finish — one mirror-diagonal row complete.
Each row scans 2×rows-1 positions — O(n²) time, O(1) extra memory.
rows = 5Trace each row’s left diagonal hit, right diagonal hit, and full line output.
i | Left hit (j) | Right hit (k) | Row output |
|---|---|---|---|
1 | j = 1 | (none) | 1 |
2 | j = 2 | k = 2 | 2 2 |
3 | j = 3 | k = 3 | 3 3 |
4 | j = 4 | k = 4 | 4 4 |
5 | j = 5 | k = 5 | 5 5 |
Row i always prints exactly two digits (one per diagonal) when rows > 1 — spaced across 2×rows-1 character positions.
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.
Each row instantly forms an inverse-V — two digits on mirror diagonals make the shape obvious.
Conditional digit-or-space printing teaches real console alignment — not abstract loop drill.
Swap digits for * to get an X-shape, or extend to Program 58’s full diamond.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace rows = 3 on paper — row 2 shows 2 2 with one space between the digits.
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 pyramid 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.
(i === j) ? i : " " with "*" in both diagonal branchesj = rows..1, append when i === j. Right: k = 2..rows, append when i === k.line string per row with += — call console.log(line) only after both inner loops finish.rows > 0 for interactive programs; rows = 1 prints a single 1 on the left diagonal.i scans 2×rows-1 positions — total work grows as O(n²) for n rows.Quick Takeaway: left j = rows..1, right k = 2..rows, digit or space, 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 pyramid is a natural follow-up to Program 56: each row places the row digit on two mirror diagonals with conditional printing. Master the fixed-rows version, then try user input and the compact 3-row trace.
Practice the three examples above, then continue to Program 58 for the full diagonal mirror diamond.
Row i prints two copies of digit i — one on each mirror diagonal across 2×rows-1 positions.
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 mirror-diagonal pyramid the beginner-friendly way.
digit i twice per row
Definitionj = rows..1
Codek = 2..rows
Codei === j or i === k
LogicO(n²) time
AnalysisEach row prints the row number twice — once on the left diagonal and once on the right — with spaces everywhere else. Total positions per row = 2×rows-1.
Move on to the full diagonal mirror diamond in the JavaScript number-pattern series.
12 people found this page helpful