Shape Rule
Mirrored diagonals
Row i logs i on the main diagonal and again on the mirrored diagonal — spaces fill every other column.

Program 53 prints a mirror diagonal number pattern: each row shows the row number on the main diagonal (left) and on a mirrored diagonal (right), forming a symmetric V-shape — a natural step after Program 52’s palindromic pyramid. This tutorial covers two inner loops with i === j and i === k conditions, a live preview, worked JavaScript examples, edge cases, and complexity.
Mirrored diagonals
Row i logs i on the main diagonal and again on the mirrored diagonal — spaces fill every other column.
i = 1..rows
for (let i = 1; i <= rows; i++) picks the current row index.
j = 1..rows
line += (i === j ? i : " ") — append the digit only on the main diagonal.
k = rows-1..1
line += (i === k ? i : " ") — mirrored diagonal; skipping the center column avoids duplication.
rows = 3..9
Pick row count and draw the V-shaped mirror diagonal pattern in the browser.
Complexity
Each row logs about 2n-1 characters — total work grows as O(n²).
A mirror diagonal number pattern prints row i with the digit i on the main diagonal and again on a mirrored diagonal — spaces fill the gaps to form a V-shape. With rows = 5, you get 1 1, 2 2, 3 3, 4 4, 5.
In JavaScript, use an outer loop for rows, then two inner loops: left half with i === j, right mirrored half with i === k, appending spaces elsewhere before console.log(line).
It bridges Program 52’s palindromic rows to conditional diagonal placement — combining nested loops with i === j logic.
i === j logs the row digit on the main diagonal.
i === k mirrors the digit on the opposite diagonal.
Program 52 uses m++/m-- for palindromic rows; Program 53 uses spacing and conditions.
Follow Program 52; continue to Program 54 next.
In short: outer for (let i = 1; i <= rows; i++), left loop for (let j = 1; j <= rows; j++) with i === j, right loop for (let k = rows - 1; k >= 1; k--) with i === k, else space, then console.log(line).
Given row count rows = 5, print a mirror diagonal number pattern — row i shows digit i on both diagonals with spaces between.
// rows = 5
// 1 1
// 2 2
// 3 3
// 4 4
// 5 | Item | Type | Description |
|---|---|---|
rows | number | How many V-shaped rows to log. |
i (outer) | number | Current row index — runs from 1 to rows. |
j (left) | number | Scans columns 1..rows; appends digit when i === j. |
k (right) | number | Scans columns rows-1..1; appends digit when i === k. |
| Cell output | string | Digit when condition matches; otherwise a space. |
| Row width | number | About 2n-1 characters per row. |
for i from 1 to rows:
line = ""
for j from 1 to rows:
append digit if i === j else space
for k from rows - 1 down to 1:
append digit if i === k else space
console.log(line) | Approach | Idea | Best for |
|---|---|---|
| Two inner loops | Left i === j, right i === k with spaces elsewhere | Learning and interviews |
| Ternary operator | i === j ? i : " " | Compact one-liners |
| User-input rows | parseInt(prompt()) | Flexible row count |
| Compact trace | rows = 3 on paper first | Quick dry-runs before full demo |
| Full X pattern | i === j || i + j === rows + 1 in one loop | Extension after mastering V-shape |
| Goal | Pattern |
|---|---|
| Outer loop | for (let i = 1; i <= rows; 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); |
| Skip center duplicate | Right loop starts at rows - 1, not rows |
| Program 52 contrast | Program 52 uses palindromic m++/m--; Program 53 uses diagonal conditions |
Same V-shape — 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
i === jMain diagonal digit placement
i === kMirrored diagonal digit placement
Reach for this pattern when teaching conditional diagonal placement, mirrored halves, and spacing in console output.
Natural follow-up after Program 52’s palindromic pyramid — introduces i === j diagonal conditions.
Each row places digits only where indices match — good bridge to matrix and grid problems.
Each row scans about 2n-1 positions — classic nested-loop O(n²) complexity.
Program 54 mirrors this V-shape downward to form a full diamond — compare the two next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in diagonal conditions, mirrored halves, and O(n²) thinking.
Choose row count between 3 and 9 and draw the mirror diagonal number pattern 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 five rows of the mirror diagonal V-shape with conditional digit placement on both diagonals.
rows = 5Hard-coded row count — append digit when i === j or i === k, otherwise append a space.
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);
} When i = 3, the left loop appends spaces until j = 3, then the right loop appends spaces until k = 3 — output 3 3. When i = 5, only the center column gets a digit because both diagonals meet at the bottom tip.
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 = 1; j <= rows; j++) {
line += (i === j ? i : " ");
}
for (let k = rows - 1; k >= 1; k--) {
line += (i === k ? i : " ");
}
console.log(line);
}
} Same diagonal two-loop core 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 conditions 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);
} 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 how many V-shaped lines log.
for (let j = 1; j <= rows; j++) — append digit when i === j, else space.
for (let k = rows - 1; k >= 1; k--) — append digit when i === k, else space.
console.log(line) after both inner loops finish the current line.
Each row prints about 2n-1 characters — O(n²) time, O(1) extra memory.
rows = 5Trace each row’s left diagonal position, right diagonal position, and full line output.
i | Left (j) | Right (k) | Row output |
|---|---|---|---|
1 | j = 1 | k = 1 | 1 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 | (none — center tip) | 5 |
Row 5 prints only one digit because the right loop starts at rows - 1, avoiding a duplicate center column.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Each row scans a fixed-width grid with conditional digit placement.
Example: trace row i = 3 in the walkthrough table.
Each row mirrors digits on two diagonals — good bridge to matrix indexing.
Example: row 5 ends with a single center digit 5 at the V tip.
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.
Each row prints about 2n-1 characters — links loops to grid traversal.
Example: 10 rows scan about 19 characters on the widest line.
Growing inner bound makes O(n²) concrete — count prints for n rows.
Example: 5 rows scan about 9 characters per line on average.
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.
Wrong conditions show up immediately as misaligned diagonals.
Each row uses real diagonal logic — 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 row i = 3 on paper — watch both loops print 3 at column 3 with spaces elsewhere.
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 number patterns.
Each character lands on its own line — you get a column, not a V-shape.
→ Use line += (i === j ? i : " ") in both loops; console.log(line) only after both inner loops.
Starting the right loop at k = rows duplicates the center digit on the bottom row.
→ Use for (let k = rows - 1; k >= 1; k--) — skip the center column.
Digits appear everywhere instead of on the diagonals only.
→ Append the digit when i === j (or i === k), not when they differ.
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.
Five rows ending with a single center 5 — 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.
m++/m-- rowsi === j diagonal conditionsi === j || i + j === rows + 1 in one column loopj = 1..rows with i === j. Right: k = rows-1..1 with i === k. Else append a space.line += ... builds the row; console.log(line) advances — call it only after both inner loops finish.rows > 0 for interactive programs; rows = 1 prints a single 1.2n-1 characters — total work is O(n²) for n rows.Quick Takeaway: outer for (let i = 1; i <= rows; i++), left i === j, right i === k, else space, then console.log(line).
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Characters per row | About 2n-1 | No storage beyond loop counters |
The mirror diagonal number pattern is a natural follow-up to Program 52: conditional digit placement on mirrored diagonals with spaces elsewhere. Master the fixed-rows version, then try user input and the compact 3-row trace.
Practice the three examples above, then continue to Program 54 to mirror this V-shape into a full diamond.
Row i logs digit i on both diagonals — left with i === j, right with i === k.
for (let j = 1; j <= rows; j++) line += (i === j ? i : " ");for (let k = rows - 1; k >= 1; k--) line += (i === k ? i : " ");rows - 1 to skip center duplicationconsole.log(line) after both inner loopsNumber.isFinite for user inputk = rows — duplicates the center digiti !== j — fills the whole row with numbersconsole.log(line) inside either inner looprows = 3 dry-run before coding rows = 5Print the V-shape the beginner-friendly way.
Digit on both diagonals per row
Definitioni === j
Codei === k
Codek = rows - 1..1
LogicO(n²) time
AnalysisEach row logs the row number on the main diagonal (left) and on a mirrored diagonal (right) using i === j and i === k. Row 3 shows 3 on both sides — about 2n-1 characters per row, so O(n²) total.
Mirror this V-shape downward to form a full diamond in the next tutorial.
12 people found this page helpful