JavaScript Number Triangle Pattern (Fill with N)

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

What Is This Pattern?

A fill-with-n triangle prints ascending digits i..n on each row, then pads with n so every line has the same width — 5 5 5 5 5, 4 5 5 5 5, …

Remember
Rule: for i = n down to 1
        print i..n, then pad with n
        (i − 1) times

5 5 5 5 5
4 5 5 5 5
3 4 5 5 5
2 3 4 5 5
1 2 3 4 5     ← n = 5

Two inner loops keep every row width n — a clear follow-up after Program 18’s growing rows.

How to Solve It

Count i down from n; print the sequence i..n, then fill remaining slots with n.

MethodIdeaBest for
Two inner loopsSequence i..n, then pad with nLearning, interviews
prompt inputSame loops; read width at runtimeInteractive practice
Custom fillPad with a separate fill constantVariant practice

Pseudocode

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

Cheat sheet

GoalPattern
Set widthconst n = 5;
Outer loopfor (let i = n; i >= 1; i--)
Sequencefor (let j = i; j <= n; j++) line += j + " ";
Fill padfor (let j = 1; j < i; j++) line += n + " ";
End rowconsole.log(line);
Custom fillline += fill + " "; in the pad loop

Printing Numbers vs Starting a New Line

APIEffectUse for
line += … + " "Stays on the same rowEach spaced number
console.log(line)Ends the lineAfter both inner loops

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

Live Preview

Change width n and the fill-with-n triangle 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 · numbers = 25
5 5 5 5 5 
4 5 5 5 5 
3 4 5 5 5 
2 3 4 5 5 
1 2 3 4 5 

Worked Walkthrough — n = 3

Trace each outer value of i, the sequence, and how many pads are added.

iSequence i..nPadsPrinted row
332× 33 3 3
22 31× 32 3 3
11 2 3none1 2 3

As i drops, the sequence grows and the pad count i − 1 shrinks — every row stays width n.

JavaScript Programs

Three complete programs: fixed n = 5, prompt input, and a custom fill constant. Use View Output for sample results, or Try It Yourself to edit and run in the playground.

Example 1 — Fixed n = 5

Hard-coded width — sequence loop then fill loop; every row has five numbers.

JavaScript
const n = 5;

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

How It Works

1. Outer loop. i runs from n down to 1 — the first number of the sequence.

2. Sequence loop. Append j from i to n with a trailing space.

3. Fill loop. Append n exactly i − 1 times so the row width stays n; then console.log(line).

Example 2 — prompt Input

Read width at runtime; fill value matches n.

JavaScript
const n = parseInt(prompt("Enter the triangle width:"), 10);

if (!Number.isFinite(n) || n < 1) {
  console.log("Please enter a positive integer.");
} else {
  for (let i = n; i >= 1; i--) {
    let line = "";
    for (let j = i; j <= n; j++) {
      line += j + " ";
    }
    for (let j = 1; j < i; j++) {
      line += n + " ";
    }
    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. Sequence end bound and fill value both use the same n.

Example 3 — Custom Fill Constant

Pad with fill = 9 while the sequence still runs up to n = 5.

JavaScript
const n = 5;
const fill = 9;

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

How It Works

1. Same structure. Outer and sequence loops match Example 1.

2. Separate fill. The pad loop appends fill instead of n.

3. Compare shapes. Last row is unchanged (no padding); upper rows show the custom pad value.

Edge Cases & Pitfalls

Check these before calling the solution done.

log inside

Vertical numbers

If console.log is inside an inner loop, each number lands on its own line. Append with +=; log only after both loops.

no fill

Uneven rows

Skipping the second loop makes top rows shorter than bottom rows.

j <= i

Wrong pad count

Use j < i (runs i − 1 times). Using j <= i adds one extra pad.

n = 1

Single number

Output is just 1 (with a trailing space) 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–3O(n²)O(n) for the current line string

Each of n rows appends exactly n numbers → n² total → O(n²).

Key Takeaways

  • Rule: print i..n, then pad with n for i − 1 slots.
  • Two loops: sequence grows as i drops; pad count shrinks.
  • Write vs log: line += … + " " builds; console.log(line) breaks.
  • Complexity: O(n²) for width n.

One line: start the sequence later on each upper row, and pad the rest with n so every line is the same width.

Frequently Asked Questions

When i = 5, the sequence loop appends j = 5 once, then the fill loop runs 4 times — all values are 5, so the row is 5 5 5 5 5.
The first inner loop appends the increasing sequence i..n. The second fills remaining positions with n so every row has the same width.
When i = 1, the sequence loop appends j = 1 to 5 and the fill loop runs zero times — no padding needed.
line += j + " " stays on the same row with a trailing space. console.log(line) prints the completed row and adds a newline.
Replace n in the second loop with a separate fill constant (see Example 3), or keep n so fill matches the width.
O(n²) for width n because each of n rows appends n numbers.
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?

Each row prints an ascending sequence i..n, then pads with n so every row has width n. The second inner loop runs i - 1 times — still O(n²) total appends.

Next: Continuous Number Triangle

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

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