A filled diamond star pattern is a centered solid diamond: an upper pyramid of odd-length star rows, mirrored below, with the widest row printed only once.
Remember
Rule: spaces = rows - i, stars = 2 * i - 1
Grow i to rows, then shrink from rows - 1
*
***
*****
*******
*********
*******
*****
***
* ← rows = 5 (half-height)
In JavaScript you build each row with two inner loops (spaces, then stars), then console.log(line). Run that once upward and once downward so the shape closes into a diamond.
Approach
How to Solve It
Reuse one row formula for both halves — start with nested loops, then optionally shorten with " ".repeat / "*".repeat.
Method
Idea
Best for
Nested loops
Spaces loop + stars loop, twice (up then down)
Learning, interviews, exams
"*".repeat
Build spaces and stars as whole strings
Shorter demos once loops click
Pseudocode
Pseudocode
for i from 1 to rows: // upper half
line = ""
append (rows - i) spaces
append (2 * i - 1) stars
console.log(line)
for i from rows - 1 down to 1: // lower half
line = ""
append (rows - i) spaces
append (2 * i - 1) stars
console.log(line)
Cheat sheet
Goal
Pattern
Upper half
for (let i = 1; i <= rows; i++)
Leading spaces
for (let j = 1; j <= rows - i; j++) line += " ";
Odd star run
for (let k = 1; k <= 2 * i - 1; k++) line += "*";
Lower half
for (let i = rows - 1; i >= 1; i--)
Total lines
2 * rows - 1
Row shortcut
console.log(" ".repeat(rows - i) + "*".repeat(2 * i - 1));
Printing Stars vs Starting a New Line
API
Effect
Use for
line += " " / line += "*"
Stays on the same line
Each space and each *
console.log(line)
Ends the current line
After spaces and stars for that row
Try it
Live Preview
Change the half-height and the diamond updates instantly — including line and star totals.
Whole numbers from 1 to 12. Printed lines = 2 * rows - 1.
Live result5 half-height · 9 lines · 41 stars
*
***
*****
*******
*********
*******
*****
***
*
Trace
Worked Walkthrough — rows = 4
Trace spaces and stars for each i. Upper grows to the middle; lower shrinks from rows - 1.
Three complete programs: fixed half-height, prompt input, and a "*".repeat shortcut. Use View Output for sample results, or Try It Yourself to edit and run in the playground.
Example 1 — Fixed rows = 5
Hard-coded half-height — ideal for first demos and screenshots.
JavaScript
let rows = 5;
for (let i = 1; i <= rows; i++) {
let line = "";
for (let j = 1; j <= rows - i; j++) {
line += " ";
}
for (let k = 1; k <= 2 * i - 1; k++) {
line += "*";
}
console.log(line);
}
for (let i = rows - 1; i >= 1; i--) {
let line = "";
for (let j = 1; j <= rows - i; j++) {
line += " ";
}
for (let k = 1; k <= 2 * i - 1; k++) {
line += "*";
}
console.log(line);
}
1. Set half-height.rows = 5 means the diamond peaks at 5 stars wide on each side of center — 9 total lines.
2. Upper half.i runs from 1 to rows. Append rows - i spaces, then 2 * i - 1 stars, then console.log(line).
3. Lower half.i runs from rows - 1 down to 1 with the same two inner loops — so the widest row is not repeated.
4. Reset each row. Start every outer iteration with line = "", or leftover characters from the previous row stick around.
Example 2 — User Input Version
Read the half-height at runtime with prompt. Validate with parseInt before looping.
JavaScript
let rows = parseInt(prompt("Enter the number of rows:"), 10);
if (!Number.isFinite(rows) || rows < 1) {
console.log("Please enter a whole number of rows >= 1.");
} else {
for (let i = 1; i <= rows; i++) {
let line = "";
for (let j = 1; j <= rows - i; j++) {
line += " ";
}
for (let k = 1; k <= 2 * i - 1; k++) {
line += "*";
}
console.log(line);
}
for (let i = rows - 1; i >= 1; i--) {
let line = "";
for (let j = 1; j <= rows - i; j++) {
line += " ";
}
for (let k = 1; k <= 2 * i - 1; k++) {
line += "*";
}
console.log(line);
}
}
1. One helper for a row.printRow builds rows - i spaces plus 2 * i - 1 stars, then logs the line.
2. Same outer structure. Call it upward for i = 1..rows, then downward from rows - 1.
3. Learn loops first. Use Examples 1–2 when you need to show nested bounds; treat this as a polish shortcut afterward.
Edge Cases & Pitfalls
Check these before calling the solution done.
Lower at rows
Doubled middle
If the lower loop starts at rows, the widest line prints twice. Start at rows - 1.
Even stars
Lost center
Use 2 * i - 1 (odd counts). Even widths break left–right symmetry.
Wrong spaces
Not centered
Spaces must be rows - i. Using i spaces leans the diamond the wrong way.
Reuse line
Growing leftovers
Reset line = "" at the start of each outer iteration, or characters from previous rows stick around.
rows = 1
Single star
Upper prints *; lower never runs — correct tiny diamond.
NaN input
Validate parseInt
Letters or empty prompt yield NaN — check Number.isFinite(rows) && rows >= 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–2)
O(rows²)
O(rows) for the current line string
"*".repeat helper (Example 3)
O(rows²)
O(rows) per temporary row string
About 2 * rows - 1 lines; each line does Θ(rows) work for spaces and stars. Total stars = rows² + (rows - 1)².
Remember
Key Takeaways
Row formula:rows - i spaces and 2 * i - 1 stars.
Two phases: grow i to rows, then shrink from rows - 1.
Break the row: append with +=; call console.log(line) after both inner loops.
Complexity:O(n²) time for half-height n; O(n) extra space for the current line string.
One line: spaces = rows - i, stars = 2*i - 1, grow then shrink from rows - 1.
Frequently Asked Questions
The first outer loop runs i from 1 to rows. On each row it appends (rows - i) spaces, then (2 * i - 1) stars. That builds the upper centered pyramid. The second outer loop runs i from (rows - 1) down to 1 with the same two inner loops, mirroring the shape so the diamond closes.
The first part already prints the widest row when i equals rows. Starting the second part at rows - 1 continues with the next narrower rows without repeating the middle line.
The filled diamond prints full runs of stars using 2*i-1 stars per row. The hollow diamond uses diagonal conditions so only the outline has stars. Both use an upper phase and a lower phase starting at rows - 1.
Odd widths keep a single center star on each row and grow by one star on each side per step, which keeps left–right symmetry.
line += " " or line += "*" stays on the same row. console.log(line) ends the current line after spaces and stars are built.
With n equal to rows (half-height), there are 2n - 1 printed lines. Each line does Theta(n) work for spaces and stars combined, so overall time is O(n²).
Exactly 2 * rows - 1 lines. The widest line has 2 * rows - 1 stars.
Use parseInt(prompt(...), 10) and check Number.isFinite(rows) && rows >= 1 so bad input does not produce NaN rows.
🤔
Did you know?
The filled diamond is Program 5’s pyramid plus its mirror: same (rows - i) spaces and (2 * i - 1) stars, with the lower half starting at rows - 1 so the widest row prints only once.