Shape Rule
Diamond halves
Top half grows 1..rows; bottom half mirrors rows-1..1.

The centered number diamond prints 1, 123, 12345, … 123456789, then mirrors back down — a natural follow-up after Program 43’s right-aligned triangle. This tutorial covers two outer loops, centering spaces, ascending digit sequences, a live preview, worked JavaScript examples, edge cases, and complexity.
Diamond halves
Top half grows 1..rows; bottom half mirrors rows-1..1.
i = 1..rows
for (let i = 1; i <= rows; i++) — builds the growing half of the diamond.
i = rows-1..1
for (let i = rows - 1; i >= 1; i--) — mirrors the top half back down.
Center rows
Leading spaces then k = 1..2*i-1 appends digits 1 to 2*i-1.
Rows 3–5
Pick a height and draw the centered number diamond in the browser.
Complexity
Each level prints O(n) digits across O(n) levels — total work scales as n².
A diamond number pyramid prints odd-length digit rows: 1, 123, 12345, … up to the widest row, then mirrors back down. With rows = 5, the output is a symmetric centered diamond.
In JavaScript you use two outer loops: the first grows rows 1..rows; the second shrinks rows-1..1. Each row appends leading spaces, then digits 1 to 2*i-1 with line += k.
It combines two-phase loops with centering — a key step after Program 43’s right-aligned triangle.
Odd-length rows.
Top + bottom loops.
rows - i spaces.
Follow Program 43; continue to Program 45 next.
In short: top loop i = 1..rows, bottom loop i = rows-1..1, spaces " ".repeat(rows - i), digits k = 1..2*i-1, then console.log(line).
Given rows = 5, print a centered number diamond: top half grows odd-length digit rows; bottom half mirrors back down.
// rows = 5
// 1
// 123
// 12345
// 1234567
//123456789
// 1234567
// 12345
// 123
// 1 | Item | Type | Description |
|---|---|---|
rows | number | Half-height of the diamond — widest row has 2*rows-1 digits. |
i | number | Outer loop — row index (top: 1..rows; bottom: rows-1..1). |
k | number | Number loop — appends digits 1..2*i-1. |
for i from 1 to rows:
append (rows - i) spaces
for k from 1 to 2*i-1: append k
console.log(line)
for i from rows-1 down to 1:
append (rows - i) spaces
for k from 1 to 2*i-1: append k
console.log(line) | Approach | Idea | Best for |
|---|---|---|
| Fixed rows | 1, 123, … | Learning and interviews |
| User-input rows | parseInt(prompt(), 10) | Configurable diamond size |
| Compact demo | rows = 3 | Paper trace before scaling up |
| Goal | Pattern |
|---|---|
| Top outer loop | for (let i = 1; i <= rows; i++) |
| Bottom outer loop | for (let i = rows - 1; i >= 1; i--) |
| Leading spaces | line += " ".repeat(rows - i) |
| Number loop | for (let k = 1; k < 2 * i; k++) |
| Append digit | line += k |
| End the row | console.log(line) |
| Program 43 contrast | Right-aligned triangle — not a centered diamond |
Same centered number diamond — different ways to control height and trace the loops.
i = 1..rowsGrowing half
i = rows-1..1Mirrored half
k < 2*iOdd-length rows
rows = 3Quick paper trace
Reach for this pattern when teaching two-phase loops, odd-length sequences, and centered console output.
Natural follow-up — moves from a right-aligned triangle to a symmetric centered diamond with two outer loops.
Practice separating top and bottom halves before tackling hollow or alphabet diamonds.
Combine loops with prompt() for flexible row counts.
Compare Program 43 (right-aligned triangle) and Program 45 (next in series) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in two-phase loops, centering spaces, and O(n²) thinking.
Choose a height between 3 and 5 and draw the centered number diamond in the browser.
Three complete JavaScript programs — fixed rows, prompt() input, and compact demo. Click View Output to reveal sample console results, or Try it Yourself to run the code live.
Print a centered diamond with rows = 5 using space loops and ascending digit sequences.
rows = 5Hard-coded row count — ideal for first demos and screenshots.
const rows = 5;
// Top half (1, 123, 12345, ...)
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);
}
// Bottom half (..., 12345, 123, 1)
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);
} When i = 1, four spaces center a single 1. When i = 3, two spaces precede 12345. The bottom half mirrors from i = 4 down to 1.
Read the row count with prompt() instead of hard-coding 5.
Read rows with prompt() and validate rows > 0 (check with Number.isFinite in real apps).
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 = " ".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);
}
} Same space-and-number loop core as Example 1; only rows comes from user input instead of being hard-coded as 5.
Run with rows = 3 to trace every row on paper before scaling up.
rows = 3Same space and number loops with a smaller row count for quick tracing.
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);
} Only rows changes from 5 to 3 — the space and number loops stay identical. Trace i = 1, 2, 3 on paper to see how row length grows as 2*i-1.
No imports needed. Set rows = 5 and loop variables i, k.
for (let i = 1; i <= rows; i++) — builds the growing half of the diamond.
line += " ".repeat(rows - i) — centers each row before digits.
for (let k = 1; k < 2 * i; k++) then line += k — odd-length digit row.
for (let i = rows - 1; i >= 1; i--) — mirrors the top half back down.
Total printed digits grow with diamond area — O(n²) time, O(1) extra memory.
rows = 5, row i = 3Trace row 3 on the top half — spaces, digits printed, and full row output.
| Step | Detail | Output so far |
|---|---|---|
| Spaces | rows - i = 2 spaces | |
k = 1..5 | Appends 12345 | 12345 |
console.log(line) | End row 3 | 12345 |
Characters per row = 2*i-1. Space count on top half = rows - i. Bottom half repeats rows 4, 3, 2, 1 in reverse.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: skip the space loop and watch the diamond snap left.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: continue to Program 45 for the next pattern in the series.
Practice line += vs console.log(line) without complex math.
Example: put console.log(line) inside the inner loop by mistake.
Add spaces between digits once the two-loop structure works.
Example: use line += k + " " between digits for wider spacing.
Diamond totals make O(n²) concrete for beginners.
Example: count printed digits for rows = 5 — top half alone prints 25 digits.
Pair the pattern with Number.isFinite and prompt() validation.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the 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 bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace i, space count, and digit loop on paper for rows = 3 before coding — watch how row length grows as 2*i-1.
Small habits that keep number-pattern code clean.
Top half 1..rows and bottom half rows-1..1 — do not repeat the peak row.
Number.isFiniteUse Number.isFinite(rows) so bad prompt() input does not crash when converting rows.
console.log Outside the Inner LoopOnly call console.log(line) after the inner loop finishes the row.
Mark rows - i spaces before each row’s digits before coding.
Trace i = 1..3 on paper before coding the full rows = 5 demo.
Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put console.log(line) inside the inner loop.
Mistakes that commonly break centered number diamond patterns.
Each digit lands on its own line — you get a column, not a diamond.
→ Use line += k; console.log(line) only after the number loop.
Without leading spaces, the diamond loses its centered shape.
→ Run the space loop before the number loop on every row.
k < 2 * i + 1 adds an extra digit — row length becomes even instead of odd.
→ Keep for (let k = 1; k < 2 * i; k++) for exactly 2*i-1 digits.
Starting the bottom loop at i = rows prints the widest row twice.
→ Bottom half starts at i = rows - 1, not rows.
parseInt(prompt())Letters or empty input yield NaN with bare parseInt(prompt()).
→ Check Number.isFinite(rows) and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 — one row, no bottom half needed.
Outer loop never runs — print nothing or show a message.
rows < 1Treat as invalid; re-prompt instead of silent empty output.
Three rows: 1, 123, 1.
Bare parseInt(prompt()) yields NaN on bad input — use Number.isFinite first.
Total lines = 2*rows - 1 — grows quadratically with peak height.
Try these variations to lock in the pattern.
rows - ii = 1..rows without the mirrork from 2*i-1 down to 1i = 1..rows, leading spaces then k = 1..2*i-1. Bottom starts at rows - 1.line +=, then console.log(line) once per row.rows > 0 for interactive programs; rows = 1 prints a single centered 1.rows - 1 — do not repeat the peak row at i = rows.Quick Takeaway: top loop i = 1..rows, leading spaces then digits k = 1..2*i-1, bottom i = rows-1..1, then console.log(line).
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Smaller demo (Example 3) | O(n²) | O(1) |
The centered number diamond is a compact lesson in symmetric patterns and centering spaces: append leading spaces, append digits 1 to 2*i-1, grow rows in the top half, then mirror back down. Master the fixed-rows version, then try prompt() input and a smaller trace demo.
Practice the three examples above, then continue to Program 45 for the next pattern in the series.
Bottom half must start at rows - 1 — validate rows when reading from the console.
for (let i = 1; i <= rows; i++)for (let i = rows - 1; i >= 1; i--)line += k for k = 1..2*i-1Number.isFinite(rows) after parseInt(prompt())console.log(line) inside the inner loopi = rows (repeats peak row)k < 2 * i + 1 instead of k < 2 * irows = 1 edge casePrint the pattern the beginner-friendly way.
Digits per row
DefinitionTop + mirror
Code2*i-1 digits
Codei = rows-1
ShapeO(n²) time
AnalysisThis pattern prints a top half (1..rows) and a bottom half (rows-1..1). Each row appends 2*i-1 digits (1 to 2*i-1) with leading spaces to center the diamond.
Move on to the next pattern in the JavaScript number-pattern series.
12 people found this page helpful