V-Shaped Hollow Star Pattern in JavaScript

Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A V-shaped hollow pattern prints only the outline of an upright V: two stars on the widest top row, then legs that meet at a single bottom vertex.

Remember
Rule: same as Program 7 — only reverse the outer loop

*       *
 *     * 
  *   *  
   * *   
    *         ← 5 rows (width 9)

It is the flip of Program 7: keep the same left/right conditions, reverse only the outer loop. That mirrors how Program 6 inverts Program 5. This outline is also the lower half of the hollow diamond.

How to Solve It

Two ways to emit the same outline — start with if/else legs, then optionally shorten with a ternary.

MethodIdeaBest for
If/else legsCountdown i; left j and right k; star when indices matchLearning, interviews, exams
Ternary ? :Same bounds; one-line star-vs-space choiceShorter demos once conditions click

Pseudocode

Pseudocode
for i from rows down to 1:
    line = ""
    for j from rows down to 1:
        line += "*" if i === j else " "
    for k from 2 to rows:
        line += "*" if i === k else " "
    print line

Cheat sheet

GoalPattern
Countdown rowsfor (let i = rows; i >= 1; i--)
Left legfor (let j = rows; j >= 1; j--) + if (i === j)
Right legfor (let k = 2; k <= rows; k++) + if (i === k)
Line width2 * rows - 1
End the rowconsole.log(line);
Ternary shortcutline += (i === j) ? "*" : " ";
Inverted Vfor (let i = 1; i <= rows; i++) → Program 7

line += vs console.log

APIEffectUse for
line += "*" / line += " "Stays on the same rowEach * and each space
console.log(line)Ends the current rowAfter both inner loops

Live Preview

Change the height and the hollow V updates instantly — including width and star count.

Whole numbers from 1 to 14. Each line is 2 * rows - 1 characters wide.

Live result 5 rows · 9 stars
*       *
 *     * 
  *   *  
   * *   
    *    

Worked Walkthrough — rows = 4

Trace where each star lands as i counts down from 4 to 1 (line width = 7).

iLeft star (j)Right star (k)StarsPrinted row
4j === 4k === 42* *
3j === 3k === 32* *
2j === 2k === 22* *
1j === 1none (k starts at 2)1*

The last row is the only single-star line — that is why the right loop must not start at k = 1. Total stars: 2 + 2 + 2 + 1 = 7 = 2×4 - 1.

JavaScript Programs

Three complete programs: countdown if/else, prompt input, and a ternary shortcut. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded height — outer loop counts down; left j = rows..1; right k = 2..rows.

JavaScript
let rows = 5;

for (let i = rows; i >= 1; i--) {
  let line = "";
  for (let j = rows; j >= 1; j--) {
    if (i === j) {
      line += "*";
    } else {
      line += " ";
    }
  }
  for (let k = 2; k <= rows; k++) {
    if (i === k) {
      line += "*";
    } else {
      line += " ";
    }
  }
  console.log(line);
}

How It Works

1. Set height. rows = 5 means five outline lines (width 9).

2. Outer loop counts down. i runs from rows (widest legs) down to 1 (bottom vertex). Start a fresh line = "" each time.

3. Left leg. j counts from rows down to 1; append * only when i === j.

4. Right leg, then print. k runs from 2 to rows with the same match rule, then console.log(line).

When i = 5 stars land at both outer columns; when i = 1 only the left loop appends a star.

Example 2 — User Input Version

Read the height at runtime with prompt. Prefer validating with Number.isFinite (shown in the tip below).

JavaScript
let rows = parseInt(prompt("Enter the number of rows:"), 10);

for (let i = rows; i >= 1; i--) {
  let line = "";
  for (let j = rows; j >= 1; j--) {
    if (i === j) {
      line += "*";
    } else {
      line += " ";
    }
  }
  for (let k = 2; k <= rows; k++) {
    if (i === k) {
      line += "*";
    } else {
      line += " ";
    }
  }
  console.log(line);
}

How It Works

1. Prompt and parse. Ask for a row count, then convert with parseInt(..., 10).

2. Same countdown core. Only the source of rows changes — the left/right leg logic matches Example 1.

3. Safer input tip. Bare parseInt yields NaN on letters or Cancel. Prefer:

Safer input
let rows = parseInt(prompt("Enter the number of rows:"), 10);
if (!Number.isFinite(rows) || rows < 1) {
  console.log("Enter a positive whole number.");
} else {
  // run the pattern loops here
}

Example 3 — Ternary ? : Form

Keep both loops and the countdown; compress the star-vs-space choice into one expression each.

JavaScript
let rows = 5;

for (let i = rows; i >= 1; i--) {
  let line = "";
  for (let j = rows; j >= 1; j--) {
    line += (i === j) ? "*" : " ";
  }
  for (let k = 2; k <= rows; k++) {
    line += (i === k) ? "*" : " ";
  }
  console.log(line);
}

How It Works

1. Same countdown. Still walk i from rows down to 1.

2. Same bounds. Left j still counts down; right k still starts at 2.

3. Shorter append. (i === j) ? "*" : " " replaces the multi-line if/else — same decision, less code.

Learn the if/else version first (Examples 1–2) so you can explain the branch in an interview; treat this as a polish shortcut afterward.

Edge Cases & Pitfalls

Check these before calling the solution done.

i++

Inverted V by mistake

If you increment i from 1 to rows, you reprint Program 7. Use i-- from rows down to 1.

k = 1

Duplicate vertex

Starting the right loop at k = 1 prints two stars on the bottom row. Keep k = 2.

console.log inside

Broken outline

If console.log is inside either inner loop, each cell lands on its own line. Use line += for cells; log only after both loops.

rows = 1

Single vertex

Output is just * — right loop never runs. A good sanity check.

rows ≤ 0

Empty output

Outer loop never runs. Validate before looping for interactive programs.

Bad prompt

Guard against NaN

Cancel or letters yield NaN — check Number.isFinite(rows) before looping.

Time and Space Complexity

ProgramTimeExtra space
If/else legs (Examples 1–2)O(rows²)O(rows) for the line string
Ternary form (Example 3)O(rows²)O(rows) for the line string

About n rows × 2n - 1 characters built per row — still quadratic in n. Total stars = 2n - 1 (same as Program 7; only print order differs).

Key Takeaways

  • Rule: countdown i; append * only when i === j or i === k.
  • Flip of Program 7: same inner loops — only reverse the outer loop.
  • Break the row: call console.log only after both inner loops.
  • Complexity: O(n²) time; O(n) for the row string.

One line: for i from rows down to 1, append a star only when the left or right index matches i — start the right loop at 2.

Frequently Asked Questions

Program 7 runs i from 1 to rows (inverted V: narrow top). Program 8 runs i from rows down to 1 with the same inner loops, so the first line uses i equal rows and prints stars at both outer columns. As i decreases, both legs move inward until the last line has a single bottom vertex.
Only the outer loop direction changes. Program 7 uses i from 1 to rows. Program 8 uses i from rows to 1. The conditions i equals j and i equals k are the same.
When i is 1, the left loop still appends a star at j equals 1. The right loop runs k from 2 to rows, so i equals k never holds on that row.
line += stays on the same row with no newline between characters. console.log(line) ends the row after both inner loops finish.
Each line has width 2 * rows - 1 characters — same geometry as Program 7, only the row order is reversed.
This page is the lower half of Program 9. Stack Program 7 on top, then this body from rows-1 down to 1, to complete the diamond.
O(n²) for n rows. Each row runs Theta(n) iterations across the two inner loops.
Use parseInt(prompt(...), 10) and check Number.isFinite(rows) && rows >= 1 so bad input does not produce NaN rows.

Did you know?

This hollow V is exactly Program 7 with the outer loop reversed — the same trick as Program 5 versus Program 6. It is the lower half of the hollow diamond. The bottom vertex is a single star because k starts at 2.

Next: Hollow Diamond

Stack the inverted V and upright V halves into a full hollow diamond.

Program 9 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful