A number-star diamond grows rows like 1, 2*2, 3*3*3 to a peak, then mirrors back down — alternating the row number and * with j % 2.
Remember
Rule: top i = 1..n, then bottom i = n-1..1
each row: 2*i-1 chars; odd j → i, even j → *
1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
…
1 ← n = 5
A natural step after Program 30’s right-aligned triangle — here you practice modulus and a two-half diamond.
Approach
How to Solve It
Grow i to n, then shrink from n-1; each row fills 2*i-1 characters with digit/* alternation.
Method
Idea
Best for
Two outer loops
Top grow + bottom mirror; shared inner alternation
Learning, interviews
prompt height
Same loops; ternary j % 2
Interactive practice
Compact n = 3
Same structure; easier to trace by hand
Paper walkthrough
Pseudocode
Pseudocode
for i from 1 to n:
line = ""
for j from 1 to 2*i - 1:
line += "*" if j even else i
print line
for i from n - 1 down to 1:
(same inner loop)
Cheat sheet
Goal
Pattern
Set height
const n = 5;
Top half
for (let i = 1; i <= n; i++)
Bottom half
for (let i = n - 1; i >= 1; i--)
Inner width
for (let j = 1; j < i * 2; j++)
Alternate
if (j % 2 === 0) line += "*"; else line += i;
Ternary
line += (j % 2 === 0) ? "*" : i;
End row
console.log(line);
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += i / line += "*"
Stays on the same row
Each character
console.log(line)
Ends the line
After the inner loop
Build the row with +=, then break once with console.log. Putting console.log inside the inner loop prints one character per line.
Try it
Live Preview
Change the height and the number-star diamond updates instantly.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultn = 5 · lines = 9
1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1
Trace
Worked Walkthrough — n = 3
Trace each half, the inner width 2*i-1, and the printed row.
Half
i
Width
Printed row
Top
1
1
1
Top
2
3
2*2
Top
3
5
3*3*3
Bottom
2
3
2*2
Bottom
1
1
1
Bottom starts at n - 1 so the peak row is not printed twice.
Code
JavaScript Programs
Three complete programs: fixed n = 5, prompt height, and compact n = 3. Use View Output for sample results, or Try It Yourself to edit and run in the playground.
Example 1 — Fixed n = 5
Hard-coded height — top grow with if/else, then bottom mirror with the same inner logic.
JavaScript
const n = 5;
for (let i = 1; i <= n; i++) {
let line = "";
for (let j = 1; j < i * 2; j++) {
if (j % 2 === 0) {
line += "*";
} else {
line += i;
}
}
console.log(line);
}
for (let i = n - 1; i >= 1; i--) {
let line = "";
for (let j = 1; j < i * 2; j++) {
if (j % 2 === 0) {
line += "*";
} else {
line += i;
}
}
console.log(line);
}
1. Top half.i runs from 1 to 5; each row gets 2*i-1 characters.
2. Alternate. Even j appends *; odd j appends the row number i.
3. Bottom half. Mirror from n - 1 down to 1 with the same inner loop — peak is not repeated.
Example 2 — prompt Height
Read n at runtime; use ternary conditions in both halves.
JavaScript
const n = parseInt(prompt("Enter n:"), 10);
if (!Number.isFinite(n) || n < 1) {
console.log("Please enter a positive integer.");
} else {
for (let i = 1; i <= n; i++) {
let line = "";
for (let j = 1; j < i * 2; j++) {
line += (j % 2 === 0) ? "*" : i;
}
console.log(line);
}
for (let i = n - 1; i >= 1; i--) {
let line = "";
for (let j = 1; j < i * 2; j++) {
line += (j % 2 === 0) ? "*" : i;
}
console.log(line);
}
}
Same if/else structure with a smaller height — easy to trace on paper.
JavaScript
const n = 3;
for (let i = 1; i <= n; i++) {
let line = "";
for (let j = 1; j < i * 2; j++) {
if (j % 2 === 0) {
line += "*";
} else {
line += i;
}
}
console.log(line);
}
for (let i = n - 1; i >= 1; i--) {
let line = "";
for (let j = 1; j < i * 2; j++) {
if (j % 2 === 0) {
line += "*";
} else {
line += i;
}
}
console.log(line);
}