Hollow Diamond Star Pattern in JavaScript

What You'll Learn
This program prints a hollow diamond by combining an inverted V (upper half, Program 7) and a V-shaped hollow pattern (lower half, Program 8).
Upper half: i = 1..rows. Lower half: i = rows - 1..1. Both halves use the same spacing and gap rules.
⭐ Pattern Output
When you run the program with rows = 5:
*
* *
* *
* *
* *
* *
* *
* *
*Complete JavaScript Program
Fixed rows = 5 version:
const rows = 5;
function printHollowVRow(i, rows) {
let line = "";
for (let s = 1; s <= rows - i; s++) line += " ";
line += "*";
if (i > 1) {
for (let g = 1; g <= 2 * i - 3; g++) line += " ";
line += "*";
}
console.log(line);
}
for (let i = 1; i <= rows; i++) {
printHollowVRow(i, rows);
}
for (let i = rows - 1; i >= 1; i--) {
printHollowVRow(i, rows);
}🧠 How It Works
Shared row helper
function printHollowVRow(i, rows) builds one line: leading spaces, left *, optional 2 * i - 3 spaces and right * when i > 1, then console.log(line). Both halves call this so the geometry never drifts.
Upper half
for (let i = 1; i <= rows; i++) printHollowVRow(i, rows); draws the inverted-V half from apex to widest row (same idea as Program 7).
Lower half (no duplicate middle)
for (let i = rows - 1; i >= 1; i--) printHollowVRow(i, rows); mirrors the upper half. Starting at rows - 1 skips the widest row a second time.
Line count and cost
Total lines: rows + (rows - 1) = 2 * rows - 1. Each line has width 2 * rows - 1. O(n²) output, O(1) extra space; wide rows scroll in the green preview on phones.
Hollow diamond
Two hollow V halves stitched with one row printer. Matches rows = 5 sample output at the top of the page.
💡 Tips for Enhancement
Try These
- Try Program 10 to print a filled diamond
- Increase
rowsto make a larger diamond - Replace
*with another character - Print spaces between stars for a wider appearance
- Try turning the hollow diamond into an outline box + diamond (Program 11)
Avoid
- Printing the middle row twice (start lower half from
rows - 1) - Using tabs for alignment (rendering differs)
- Mixing
with normal spaces (output becomes inconsistent) - Printing a second star for
i = 1(it duplicates the top/bottom point) - Forgetting the line break after each row
Key Takeaways
Upper half prints i = 1..rows.
Lower half prints i = rows - 1..1 to avoid duplication.
Each row uses leading spaces and an inner gap to place border stars.
Inner gap is 2*i - 3 for i > 1.
Time complexity is O(n²) due to printing \(\Theta(n^2)\) characters.
❓ Frequently Asked Questions
i, it is 2*i - 3 (only when i > 1).n rows, since total printed characters are \(\Theta(n^2)\).Next: Filled Diamond
Continue to Program 10 to print a filled diamond star pattern.
Once you can print a hollow diamond, a filled diamond is just changing the inner-gap logic into star printing.
9 people found this page helpful
