Unlike Program 43 (right-aligned triangle), this pattern is symmetric — total lines for n levels is 2n - 1. Cap at 5 so digits stay single (1–9).
Approach
How to Solve It
Build the top half, then mirror it downward — spaces first, then digits concatenated with no gaps. End each row with console.log.
Method
Idea
Best for
Two-half diamond
Top 1..rows, bottom rows-1..1
Classic diamond demos
Compact
Same logic with a smaller rows (e.g. 3)
Quick dry-runs
Pseudocode
Pseudocode
for i from 1 to rows:
line = spaces of length (rows - i)
for k from 1 to (2*i - 1):
append k
print line
for i from rows-1 down to 1:
line = spaces of length (rows - i)
for k from 1 to (2*i - 1):
append k
print line
Cheat sheet
Goal
Pattern
Top half
for (let i = 1; i <= rows; i++)
Bottom half
for (let i = rows - 1; i >= 1; i--)
Leading spaces
let line = " ".repeat(rows - i);
Odd-length digits
for (let k = 1; k < 2 * i; k++) line += k;
End the row
console.log(line);
Total lines
2 * rows - 1
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += …
Stays on the same row
Spaces and each digit
console.log(line)
Ends the current row
After spaces and digits finish
Append without a newline, then end the row once.
Try it
Live Preview
Change the level count and the number diamond updates instantly.
Whole numbers from 1 to 5 (peak stays within single digits 1–9). Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 9 lines
1
123
12345
1234567
123456789
1234567
12345
123
1
Trace
Worked Walkthrough
Trace spaces and digits when rows = 3 — up, then back down.
Half
i
Spaces / digits
Printed row
Top
1
2 / 1
1
Top
2
1 / 123
123
Top
3
0 / 12345
12345
Bottom
2
1 / 123
123
Bottom
1
2 / 1
1
Total lines = 2 * rows - 1. Digit count on row i = 2 * i - 1.
Code
JavaScript Programs
Three complete programs: fixed rows = 5, prompt input, and a compact 3-level demo. Use View Output for samples, or Try It Yourself to edit and run live.
Example 1 — Fixed rows = 5
Top half grows to 123456789; bottom half mirrors back to 1.
JavaScript
const rows = 5;
for (let i = 1; i <= rows; i++) {
let line = " ".repeat(rows - i);
for (let k = 1; k < 2 * i; k++) {
line += k;
}
console.log(line);
}
for (let i = rows - 1; i >= 1; i--) {
let line = " ".repeat(rows - i);
for (let k = 1; k < 2 * i; k++) {
line += k;
}
console.log(line);
}
1. Grow the top. Outer loop i = 1..rows — each row gets rows - i spaces, then digits 1..(2*i-1).
2. Mirror the bottom. Second loop runs i = rows-1..1 with the same space and digit rules.
3. Concatenate digits.line += k (no space) keeps the row as a solid digit string like 12345.
Example 2 — User Input Levels
Read the level count with prompt and validate before drawing.
JavaScript
const rows = parseInt(prompt("Enter number of levels (1-5):"), 10);
if (!Number.isFinite(rows) || rows < 1 || rows > 5) {
console.log("Please enter an integer from 1 to 5.");
} else {
for (let i = 1; i <= rows; i++) {
let line = " ".repeat(rows - i);
for (let k = 1; k < 2 * i; k++) {
line += k;
}
console.log(line);
}
for (let i = rows - 1; i >= 1; i--) {
let line = " ".repeat(rows - i);
for (let k = 1; k < 2 * i; k++) {
line += k;
}
console.log(line);
}
}
1. Prompt and validate. Cap at 5 so the peak digit stays within 1–9.
2. Same diamond core. Only the source of rows changes — both halves match Example 1.
3. Entering 3. Five lines total (2*3 - 1), peaking at 12345.
Example 3 — Compact rows = 3
Same two-half logic with a smaller height for a quick visual check.
JavaScript
const rows = 3;
for (let i = 1; i <= rows; i++) {
let line = " ".repeat(rows - i);
for (let k = 1; k < 2 * i; k++) {
line += k;
}
console.log(line);
}
for (let i = rows - 1; i >= 1; i--) {
let line = " ".repeat(rows - i);
for (let k = 1; k < 2 * i; k++) {
line += k;
}
console.log(line);
}
1. Shrink the height. Only rows changes — still two outer loops and odd-length digit rows.
2. Easier to trace. Five lines instead of nine — useful while learning the space formula.
3. Same peak rule. Middle row still has 2*rows - 1 digits (12345 when rows = 3).
Edge Cases & Pitfalls
Check these before calling the solution done.
Odd width
Use k < 2 * i (or k <= 2*i - 1)
Looping only to i makes rows 1, 12, 123… instead of odd lengths.
rows > 5
Single-digit peak breaks past 5
line += k prints 10, 11… and columns break. Cap at 5, or use fixed-width formatting.
rows = 1
Single-level case
Output is just 1 — the bottom loop never runs.
Bad input
Validate parseInt
Check Number.isFinite(rows) and require 1 <= rows <= 5 before the loops.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / compact (Examples 1, 3)
O(n²)
O(n) per row string
User input (Example 2)
O(n²)
O(n) per row string
There are 2n - 1 rows and up to O(n) digits per row, so total work is quadratic in the level count.
Remember
Key Takeaways
Rule: grow i to rows, then shrink; each row appends 1..(2*i-1).
Center: leading spaces of length rows - i on both halves.
Break the row: call console.log(line) only after spaces and digits finish.
Complexity:O(n²) time — 2n-1 rows with up to O(n) digits each.
One line: pad left, append odd-length digits up to the peak, then mirror the same rows downward.
Frequently Asked Questions
A centered diamond of ascending digits: 1, 123, 12345, 1234567, 123456789, then the same rows mirrored back down to 1.
The row appends 2*i-1 digits (odd length): 1, 3, 5, 7, 9, … using for (k = 1; k < 2 * i; k++).
Leading spaces via " ".repeat(rows - i) before the digit loop on both halves.
The first builds the top half (i = 1..rows). The second mirrors back down (i = rows-1..1) to complete the diamond.
line += appends on the same row. console.log(line) ends the row after spaces and digits finish.
Program 43 is a right-aligned triangle with spaced columns. Program 44 is a symmetric centered diamond with odd-length digit rows.
O(n²) for n levels because each level appends O(n) digits and there are O(n) rows overall.
Only one row prints — a single centered 1. The bottom loop never runs.
🤔
Did you know?
Top half runs i = 1..rows; bottom half mirrors i = rows-1..1. Each row appends 2*i-1 digits (1 to 2*i-1) with leading spaces. Total lines = 2*rows - 1.