Shape Rule
Full diamond
Top half i = k..1, bottom half i = 2..k — complete symmetric diamond.

Program 47 prints a full concentric number diamond: top half peels from k to 1, then the bottom half mirrors from 2 back to k — a natural step after Program 46’s top-half square. This tutorial covers two outer loops with j > i logic, a live preview, worked JavaScript examples, edge cases, and complexity.
Full diamond
Top half i = k..1, bottom half i = 2..k — complete symmetric diamond.
i = k..1
for (let i = k; i >= 1; i--) builds layers from outside down to center.
i = 2..k
for (let i = 2; i <= k; i++) mirrors rows back out — skip i = 1 (already printed).
j > i
j > i ? j : i — append outer column j or current layer i.
k = 3..7
Pick outer value k and draw the full concentric diamond in the browser.
Complexity
k top rows + k - 1 bottom rows × 2k - 1 columns — total ≈ (2k-1)² prints.
A concentric number diamond prints layers that decrease toward the center, then mirror back out to form a complete symmetric shape. With k = 5, the grid is 9 × 9 — the center cell is 1.
In JavaScript two outer loops handle top (i = k..1) and bottom (i = 2..k) halves; each row uses two inner loops and the j > i rule with line +=.
It extends Program 46 with a bottom-half loop — the key step from half-pattern to full diamond symmetry.
Top k..1, bottom 2..k.
Two inner loops per row.
Program 46 is top half only; Program 47 adds the bottom mirror.
Follow Program 46; continue to Program 48 next.
In short: top loop i = k..1, bottom loop i = 2..k, cell rule j > i ? j : i, then console.log(line).
Given outer value k = 5, print a full concentric number diamond — top half peels to 1, bottom half mirrors back out.
// k = 5 (9x9)
//5 5 5 5 5 5 5 5 5
//5 4 4 4 4 4 4 4 5
//5 4 3 3 3 3 3 4 5
//5 4 3 2 2 2 3 4 5
//5 4 3 2 1 2 3 4 5
//5 4 3 2 2 2 3 4 5
//5 4 3 3 3 3 3 4 5
//5 4 4 4 4 4 4 4 5
//5 5 5 5 5 5 5 5 5 | Item | Type | Description |
|---|---|---|
k | number | Outer (maximum) number — also sets row count and half-width. |
i (top) | number | Top outer loop — layer value from k down to 1. |
i (bottom) | number | Bottom outer loop — layer value from 2 up to k. |
j | number | Inner loop — column index for left (k..1) or right (2..k) half. |
| Grid size | number | 2 × k - 1 rows and columns (9 when k = 5). |
for (let i = k; i >= 1; i--) { // top half
let line = "";
for (let j = k; j >= 1; j--) line += (j > i ? j : i) + " ";
for (let j = 2; j <= k; j++) line += (j > i ? j : i) + " ";
console.log(line);
}
for (let i = 2; i <= k; i++) { // bottom half
let line = "";
for (let j = k; j >= 1; j--) line += (j > i ? j : i) + " ";
for (let j = 2; j <= k; j++) line += (j > i ? j : i) + " ";
console.log(line);
} | Approach | Idea | Best for |
|---|---|---|
| Two outer loops + ternary | 9×9 diamond for k = 5 | Learning and interviews |
| User-input k | parseInt(prompt()) | Flexible outer value |
| Ternary operator | j > i ? j : i | Compact one-liner per cell |
| Goal | Pattern |
|---|---|
| Top half | for (let i = k; i >= 1; i--) |
| Bottom half | for (let i = 2; i <= k; i++) |
| Left half | for (let j = k; j >= 1; j--) |
| Right half | for (let j = 2; j <= k; j++) |
| Cell rule | line += (j > i ? j : i) + " " |
| Ternary form | line += (j > i ? j : i) + " " |
| Grid size | 2 × k - 1 rows and columns |
| Program 46 contrast | Top-half square only — Program 47 adds bottom mirror loop |
Same full diamond — different ways to set k and trace the two outer loops.
i = k..1Peel toward center
i = 2..kMirror back out
j = k..1Descending columns
j = 2..kMirror without center dup
j > i ? j : iOuter or layer value
Reach for this pattern when teaching symmetric output, layer logic, and dual inner loops.
Natural follow-up after Program 46 — adds the bottom-half outer loop for a complete diamond.
Top (k..1) and bottom (2..k) teach full vertical symmetry.
j > i selects which concentric ring each cell belongs to.
Compare Program 46 (top half) and Program 48 (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 nested loops, symmetry, and O(k²) thinking.
Choose outer value k between 3 and 7 and draw the full concentric diamond in the browser.
Three complete JavaScript programs — fixed k = 5, prompt() input, and compact k = 3 trace demo. Click View Output to reveal sample console results, or Try it Yourself to run the code live.
Print a full concentric number diamond with k = 5 using two outer loops and the j > i rule.
k = 5Hard-coded outer value — top half then bottom half for a complete 9×9 diamond.
const k = 5;
for (let i = k; i >= 1; i--) {
let line = "";
for (let j = k; j >= 1; j--) {
line += (j > i ? j : i) + " ";
}
for (let j = 2; j <= k; j++) {
line += (j > i ? j : i) + " ";
}
console.log(line);
}
for (let i = 2; i <= k; i++) {
let line = "";
for (let j = k; j >= 1; j--) {
line += (j > i ? j : i) + " ";
}
for (let j = 2; j <= k; j++) {
line += (j > i ? j : i) + " ";
}
console.log(line);
} The first outer loop prints rows i = 5..1 (top half). The second prints i = 2..5 (bottom half) — row i = 1 is skipped because it was already the center row.
Read outer value k from the console instead of hard-coding 5.
Read k with prompt() and reject non-positive values — both halves adjust automatically.
const kInput = prompt("Enter k:");
const k = parseInt(kInput, 10);
if (!Number.isFinite(k) || k < 1) {
console.log("Please enter a positive integer.");
} else {
for (let i = k; i >= 1; i--) {
let line = "";
for (let j = k; j >= 1; j--) {
line += (j > i ? j : i) + " ";
}
for (let j = 2; j <= k; j++) {
line += (j > i ? j : i) + " ";
}
console.log(line);
}
for (let i = 2; i <= k; i++) {
let line = "";
for (let j = k; j >= 1; j--) {
line += (j > i ? j : i) + " ";
}
for (let j = 2; j <= k; j++) {
line += (j > i ? j : i) + " ";
}
console.log(line);
}
} Same two-pass structure as Example 1; only the source of k changes. Grid size becomes 2k - 1 rows and columns.
Smaller outer value for quick tracing — 5 rows total (3 top + 2 bottom).
k = 3Use k = 3 to trace both outer loops quickly on paper or in interviews.
const k = 3;
for (let i = k; i >= 1; i--) {
let line = "";
for (let j = k; j >= 1; j--) {
line += (j > i ? j : i) + " ";
}
for (let j = 2; j <= k; j++) {
line += (j > i ? j : i) + " ";
}
console.log(line);
}
for (let i = 2; i <= k; i++) {
let line = "";
for (let j = k; j >= 1; j--) {
line += (j > i ? j : i) + " ";
}
for (let j = 2; j <= k; j++) {
line += (j > i ? j : i) + " ";
}
console.log(line);
} Five rows total — three from the top loop, two from the bottom (skipping center duplicate). Easy to dry-run before scaling to k = 5.
No imports needed. Set k = 5 as the outer value and row/layer count.
for (let i = k; i >= 1; i--) walks layers from outside down to center row.
If j > i print j; else print i — builds descending left side.
for (let i = 2; i <= k; i++) builds rows back out — skips i = 1 (center already done).
console.log(line) after both inner loops finish each row.
Grid size 2k - 1 × 2k - 1 — O(k²) time, O(1) extra memory.
i = 3, k = 5Trace left-half columns j on row 3 — which value prints for each cell.
j | j > i? | Prints |
|---|---|---|
5 | Yes | 5 |
4 | Yes | 4 |
3 | No | 3 (i) |
2 | No | 3 (i) |
1 | No | 3 (i) |
Left half of center row (i = 1): 5 4 3 2 1. Right half mirrors to 2 3 4 5 — full center row: 5 4 3 2 1 2 3 4 5. Bottom loop then prints rows i = 2..5 to complete the 9×9 diamond.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: change k to 3 for a quick trace — see Example 3.
Foundation for concentric layers, symmetric grids, and peel-down patterns.
Example: continue to Program 48 for the next pattern in the series.
Practice building a line string vs calling console.log() without complex math.
Example: put console.log() inside the inner loop by mistake.
Two inner loops teach left-right mirroring without string reversal.
Example: trace row i = 3 in the walkthrough table.
2k - 1 rows × 2k - 1 columns makes O(k²) concrete.
Example: count cells for k = 5 — 9 × 9 = 81 prints.
Pair the pattern with Number.isFinite() and positive-row checks.
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.
Change k, use ternary form, or trace with k = 3 for quick dry-runs.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace row i = 3 on paper — watch how j > i switches from outer values to the current layer.
Small habits that keep number-pattern code clean.
Never hard-code 5 in loop bounds — use k everywhere.
Number.isFinite()Avoid crashes when the user types letters instead of a number for k.
Only call console.log(line) after both inner loops finish the row.
Left half uses j = k..1; right half uses j = 2..k — do not repeat j = 1 on the right.
Trace all three rows on paper before coding the full k = 5 demo.
Pro Tip: if the output is a vertical list of single numbers per line, you almost certainly put console.log() inside the inner loop.
Mistakes that commonly break concentric number diamond patterns.
Each cell lands on its own line — you get a column, not a diamond.
→ Use line += (j > i ? j : i) + " " per cell; console.log(line) only after both inner loops.
Starting the right half at j = 1 duplicates the center digit on every row.
→ Use for (let j = 2; j <= k; j++) for the mirror half.
Only the top loop runs — output stops at the center row like Program 46.
→ Add for (let i = 2; i <= k; i++) with the same inner loops after the top half.
k = 1 prints a single 1; k = 2 gives a minimal 3×3 diamond.
→ Validate k >= 2 for interactive programs expecting a visible pattern.
parseInt(prompt())Letters or empty input return NaN with bare parseInt(prompt()).
→ Catch ValueError and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 on one line — no layers to peel.
Outer loop never runs — print nothing or show a message.
k < 0Treat as invalid; re-prompt instead of silent empty output.
3×3 grid — 2 2 2, 2 1 2, 2 2 2.
Bare parseInt(prompt()) returns NaN on bad input — use Number.isFinite() first.
Each row prints 2k - 1 cells — total work grows as k².
Try these variations to lock in the pattern.
i = 2, not i = 1i = k..1. Bottom: i = 2..k (skip center duplicate). Same inner loops and cell rule in both.line string; call console.log(line) only after both inner loops finish.k > 0 for interactive programs; k = 1 prints a single 1.2k - 1 rows × 2k - 1 columns — total prints ≈ (2k - 1)².Quick Takeaway: top loop i = k..1, bottom loop i = 2..k, cell rule j > i ? j : i, then console.log(line).
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(k²) | O(1) |
| Smaller demo (Example 3) | O(k²) | O(1) |
The concentric number diamond extends Program 46 with a bottom-half outer loop: top (i = k..1) plus bottom (i = 2..k) using the same j > i cell rule. Master the fixed-k version, then try user input and the compact k = 3 trace.
Practice the three examples above, then continue to Program 48 for the next pattern in the series.
Two outer loops — top k..1, bottom 2..k — skip i = 1 in the bottom loop to avoid duplicating the center row.
for (let i = k; i >= 1; i--), Bottom: for (let i = 2; i <= k; i++)j = k..1, Right: j = 2..k per rowj if j > i, else i to linei = 1 in bottom loop (center already printed)Number.isFinite(k) after parseInt(prompt())console.log() inside the inner cell loopi = 1 — duplicates center rowk = 3 dry-run before coding k = 5Print the pattern the beginner-friendly way.
Two outer loops
Definitioni = k..1
Codei = 2..k
Codej > i ? j : i
Logic(2k-1)² cells
AnalysisPrint top half with for (let i = k; i >= 1; i--), then bottom half with for (let i = 2; i <= k; i++). Each cell: j > i ? j : i. Grid size = 2k - 1 rows and columns.
Move on to the next pattern in the JavaScript number-pattern series.
12 people found this page helpful