A diagonal mirror number pyramid prints digit i on a left diagonal and again on a right diagonal — spaces fill the gaps, forming an inverse-V that widens downward.
Remember
Rule: digit when i === j (left) or i === k (right)
1
2 2
3 3
4 4
5 5 ← rows = 5
Unlike Program 56 (full palindrome 1..i..1), only the row digit appears — about 2n - 1 characters per row.
Approach
How to Solve It
Left loop counts down from rows; right loop counts up from 2 — each places the digit or a space.
Method
Idea
Best for
Two-half diagonals
j = rows..1 left, k = 2..rows right
Classic inverse-V demos
Compact
Same logic with fewer rows (e.g. 3)
Quick dry-runs
Pseudocode
Pseudocode
for i from 1 to rows:
line = ""
for j from rows down to 1: // left half
append i if i === j else " "
for k from 2 to rows: // right half
append i if i === k else " "
print line
Cheat sheet
Goal
Pattern
Outer (rows)
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 the row
console.log(line);
Chars per row
2 * rows - 1
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += …
Stays on the same row
Each digit or space
console.log(line)
Ends the current row
After both halves finish
Append without a newline, then end the row once.
Try it
Live Preview
Change the height and the inverse-V diagonals update instantly.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 45 chars
1
2 2
3 3
4 4
5 5
Trace
Worked Walkthrough
Trace three rows when rows = 5 — watch where i === j and i === k fire.
Row i
Left at j
Right at k
Printed row
1
j = 1
(none)
1
3
j = 3
k = 3
3 3
5
j = 5
k = 5
5 5
The gap between the two digits grows with i — that is the inverse-V shape.
Code
JavaScript Programs
Three complete programs: fixed rows = 5, prompt input, and a compact rows = 3 demo. Use View Output for samples, or Try It Yourself to edit and run live.
Example 1 — Fixed rows = 5
Append the digit when i === j or i === k; otherwise append a space.
JavaScript
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);
}
1. Left half places the first digit. Count j down from rows; append i when j matches, else a space.
2. Right half mirrors it. Count k from 2 to rows so the center column is not printed twice.
3. Gap grows with the row. Lower rows place the two digits farther apart — that forms the inverse V.
Example 2 — User Input Rows
Read rows with prompt and validate before drawing.
JavaScript
const rows = parseInt(prompt("Enter number of rows:"), 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);
}
}
1. Prompt and validate. Use parseInt and require rows >= 1.
2. Same two-half core. Only the source of rows changes — left and right diagonal loops match Example 1.
3. Entering 4. Each row has 7 characters (2×4 - 1), base row 4 4.
Example 3 — Compact rows = 3
Same diagonal logic with fewer rows for a quick visual check.
JavaScript
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);
}