Inverted Right-Aligned Star Pattern in JavaScript

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

What Is This Pattern?

An inverted right-aligned triangle shrinks star counts while staying flush on the right: row i has i - 1 leading spaces and rows - i + 1 stars.

Remember
Rule: spaces = i - 1, stars = rows - i + 1

*****
 ****
  ***
   **
    *     ← 5 rows (spaces shown as blanks)

It combines Program 2’s shrinking stars with Program 3’s right alignment. Every row still has width rows before the newline.

How to Solve It

Two inner loops per row — spaces then stars — or the same formulas with String.repeat.

MethodIdeaBest for
Nested loopsj < i spaces, then k = i..rows starsLearning, interviews, exams
String.repeatBuild spaces and stars as whole stringsShorter demos once loops click

Pseudocode

Pseudocode
for i from 1 to rows:
    line = ""
    for j from 1 to i - 1:     // i - 1 spaces
        line += " "
    for k from i to rows:       // rows - i + 1 stars
        line += "*"
    print line

Cheat sheet

GoalPattern
Walk each rowfor (let i = 1; i <= rows; i++)
Leading spacesfor (let j = 1; j < i; j++) line += " ";
Shrinking starsfor (let k = i; k <= rows; k++) line += "*";
Star count formfor (let k = 1; k <= rows - i + 1; k++)
Fixed width check(i - 1) + (rows - i + 1) === rows
Row shortcutconsole.log(" ".repeat(i - 1) + "*".repeat(rows - i + 1));

line += vs console.log

APIEffectUse for
line += " " / line += "*"Stays on the same rowEach space and each *
console.log(line)Ends the current rowAfter spaces and stars for that row

Live Preview

Change the row count and the inverted right-aligned triangle updates instantly.

Whole numbers from 1 to 20. Each row has width rows (spaces + stars).

Live result 5 rows · 15 stars
*****
 ****
  ***
   **
    *

Worked Walkthrough — rows = 4

Trace spaces, stars, and total width for each outer-loop value of i.

iSpaces i - 1Stars rows - i + 1WidthPrinted row
1044****
2134***
3224**
4314*

Total stars: 4 + 3 + 2 + 1 = 10 = 4×5/2. Width stays 4 on every row.

JavaScript Programs

Three complete programs: fixed rows, prompt input, and a String.repeat shortcut. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded 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 < i; j++) {
    line += " ";
  }
  for (let k = i; k <= rows; k++) {
    line += "*";
  }
  console.log(line);
}

How It Works

1. Set height. rows = 5 means five lines, each of width 5.

2. Outer loop picks the row. i runs from 1 to rows. Start a fresh line = "" each time.

3. Spaces then stars. Append i - 1 spaces (j < i), then stars for k = i..rows (that is rows - i + 1 stars).

4. Print the row. console.log(line) after both inner loops starts the next row.

When i = 1: 0 spaces + 5 stars. When i = 5: 4 spaces + 1 star.

Example 2 — User Input Version

Read the row count 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 = 1; i <= rows; i++) {
  let line = "";
  for (let j = 1; j < i; j++) {
    line += " ";
  }
  for (let k = i; k <= rows; k++) {
    line += "*";
  }
  console.log(line);
}

How It Works

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

2. Same nested-loop core. Only the source of rows changes — the print 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 — String.repeat + Explicit Count

Name the space and star counts, then build each row in one call.

JavaScript
let rows = 5;

for (let i = 1; i <= rows; i++) {
  let spaces = i - 1;
  let stars = rows - i + 1;
  console.log(" ".repeat(spaces) + "*".repeat(stars));
}

How It Works

1. Compute both counts. spaces = i - 1 and stars = rows - i + 1 make the invert-and-align rule obvious.

2. Build and print. Concatenate the two repeat results and log the full row.

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.

j <= i

One extra space

Space loop must be j < i (exactly i - 1 spaces). j <= i breaks the right edge.

Program 3 formulas

Grows instead

rows - i spaces and 1..i stars is Program 3. Here use i - 1 and rows - i + 1.

No spaces

Left-aligned invert

Skipping the space loop gives Program 2. Right alignment needs leading spaces.

console.log inside

Column of characters

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

rows = 1

Single star

0 spaces + 1 star — same tip case as the other triangle pages.

Bad prompt

Guard against NaN

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

Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–2)O(rows²)O(rows) for the line string
String.repeat shortcut (Example 3)O(rows²)O(rows) per temporary row string

Each of n rows builds Θ(n) characters (spaces + stars). Star count alone is still n(n+1)/2.

Key Takeaways

  • Formulas: i - 1 spaces and rows - i + 1 stars.
  • Fixed width: spaces + stars = rows on every line.
  • Break the row: line += for spaces/stars; console.log after both loops.
  • Complexity: O(n²) time; O(n) for the row string.

One line: append i - 1 spaces, then rows - i + 1 stars — inverted and flush right.

Frequently Asked Questions

For each row i from 1 to rows, append i minus 1 spaces, then append stars with k running from i to rows inclusive. That prints rows minus i plus 1 stars. Row 1 has no spaces and rows stars; each later row adds one space and removes one star while keeping the same right edge.
The range i through rows has length rows minus i plus 1, which matches the star count. An equivalent loop is k from 1 to rows minus i plus 1.
Program 3 uses (rows - i) spaces and stars 1 through i. Program 4 uses (i - 1) spaces and stars i through rows. Same right alignment; star counts grow in Program 3 and shrink in Program 4.
Program 2 is left-aligned with shrinking stars. Program 4 adds growing leading spaces so the same shrinking star counts stay flush on the right.
line += stays on the same row with no newline between characters. console.log(line) ends the row after both inner loops finish.
O(n²) for n rows. Each row prints on the order of n characters; there are n rows.
Yes. console.log(' '.repeat(i - 1) + '*'.repeat(rows - i + 1)) builds each row without explicit inner character loops.
j from 1 to i-1 (written as j < i) prints exactly i - 1 spaces. Using j <= i would add one extra space and break the right edge.

Did you know?

This pattern merges Program 2’s shrinking star count with Program 3’s right alignment. Every row still has width rows: (i - 1) + (rows - i + 1) = rows.

Next: Center Pyramid

Use leading spaces and odd star counts (2 * i - 1) to print a full pyramid.

Program 5 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