JavaScript Repeating Number Pattern (Shrinking)

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

What Is This Pattern?

A shrinking repeating pattern starts wide and shortens as the digit grows — 11111, 2222, 333, 44, 5.

Remember
Rule: for i = 1 to n
        print i, (n − i + 1) times

11111
2222
333
44
5         ← n = 5

Twin of Program 9 (1, 22, 333): same ascending digit, but the repeat count shrinks instead of growing.

How to Solve It

Count the digit up from 1; on each row, append that digit n − i + 1 times.

MethodIdeaBest for
Nested loopsOuter i = 1..n; inner j = i..nLearning, interviews
prompt inputSame loops; read n at runtimeInteractive practice
String.repeatString(i).repeat(n − i + 1)Shorter demos

Pseudocode

Pseudocode
for i from 1 to n:
    line = ""
    for j from i to n:
        line += i
    print line (with newline)

Cheat sheet

GoalPattern
Set sizeconst n = 5;
Outer loopfor (let i = 1; i <= n; i++)
Inner loopfor (let j = i; j <= n; j++) line += i;
End rowconsole.log(line);
Repeat countn - i + 1
Shortcutconsole.log(String(i).repeat(n - i + 1));

Printing Numbers vs Starting a New Line

APIEffectUse for
line += iStays on the same rowEach repeated digit
console.log(line)Ends the lineAfter the inner loop

Build the row with +=, then break once with console.log. Putting console.log inside the inner loop prints one digit per line.

Live Preview

Change n and the shrinking repeating pattern updates instantly.

Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result n = 5 · digits = 15
11111
2222
333
44
5

Worked Walkthrough — n = 3

Trace each outer value of i and how many times it is printed.

iRepeats n−i+1Printed row
13× print 1111
22× print 222
31× print 33

As i rises, the inner loop starts later — fewer copies of a larger digit.

JavaScript Programs

Three complete programs: fixed n = 5, prompt input, and a String.repeat shortcut. 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 — outer picks digit; inner controls how many times to print it.

JavaScript
const n = 5;

for (let i = 1; i <= n; i++) {
  let line = "";
  for (let j = i; j <= n; j++) {
    line += i;
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Outer loop. i runs from 1 to n — the digit for that row.

2. Inner loop. j runs from i to n, so line += i runs n − i + 1 times.

3. Newline. console.log(line) after the inner loop prints the row and starts the next one.

Example 2 — prompt Input

Read n at runtime with prompt and parseInt.

JavaScript
const n = parseInt(prompt("Enter the number of rows:"), 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 = i; j <= n; j++) {
      line += i;
    }
    console.log(line);
  }
}
Try It Yourself

How It Works

1. Prompt and parse. Convert the answer with parseInt(..., 10).

2. Validate first. Reject NaN or non-positive values before looping.

3. Same loops. Only the source of n changes — print i, n − i + 1 times.

Example 3 — String.repeat

Build each row in one call — same shape, no explicit inner loop.

JavaScript
const n = 5;

for (let i = 1; i <= n; i++) {
  console.log(String(i).repeat(n - i + 1));
}
Try It Yourself

How It Works

1. One outer loop. Still walk i from 1 to n.

2. Build the row. String(i).repeat(n − i + 1) creates the repeated digit string.

3. Print and advance. console.log prints that string and ends the line.

Edge Cases & Pitfalls

Check these before calling the solution done.

log inside

Vertical digits

If console.log is inside the inner loop, each digit lands on its own line. Append with +=; log only after the inner loop.

print(j)

Wrong digits

Appending j instead of i prints a sequence, not a repeated digit.

j = 1..i

Program 9 shape

Using j = 1..i instead of j = i..n yields 1, 22, 333 — that is Program 9.

n = 1

Single digit

Output is just 1 on one line.

n ≤ 0

Empty output

The outer loop never runs. Guard prompt input with n >= 1.

NaN input

Validate parseInt

Letters or empty prompt yield NaN — check Number.isFinite(n) && n >= 1.

Time and Space Complexity

ProgramTimeExtra space
Examples 1–2O(n²)O(n) for the current line string
String.repeat (Example 3)O(n²)O(n) per temporary row string

Total digits printed: n + (n−1) + … + 1 = n(n+1)/2 → still O(n²).

Key Takeaways

  • Rule: outer i = 1..n; print i exactly n − i + 1 times.
  • Vs Program 9: digit grows; repeat count shrinks.
  • Write vs log: line += i builds; console.log(line) breaks.
  • Complexity: O(n²) for n rows.

One line: walk the digit up from 1, and print it one fewer time on each new row.

Frequently Asked Questions

The inner loop runs from j = i to n. As i increases (1, 2, 3, …), the inner loop runs fewer times, so each row prints fewer copies of the row digit.
On the first row, i = 1 and the inner loop runs from 1 to n, so it prints 1 exactly n times — five 1s when n = 5.
line += i stays on the same row while building digits. console.log(line) prints the completed row and adds a newline.
Program 11 counts down and repeats digit i exactly i times (55555, 4444, 333). Program 12 counts up and repeats digit i exactly n − i + 1 times (11111, 2222, 333, 44, 5).
O(n²) where n is the number of rows. Total digit appends equal n+(n−1)+…+1 = n(n+1)/2.
Yes. console.log(String(i).repeat(n − i + 1)) prints a full row in one call. Nested loops are better for learning; String.repeat is a handy shortcut later.
Use parseInt(prompt(...), 10) and check Number.isFinite(n) && n >= 1 so bad input does not produce NaN.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.

Did you know?

Row digit i repeats n − i + 1 times. The outer loop counts up from 1, while the inner loop runs from i to n, so each row shrinks — still O(n²) total prints.

Next: Alternating Rows

Continue with the next pattern in the JavaScript number-pattern series.

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