JavaScript Number Pattern (Increasing Suffix)

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

What Is This Pattern?

A reverse ascending number triangle starts each row at a falling start digit and counts up to rows — digits grow on the left: 5, 45, 345, …

Remember
Rule: outer i = rows..1, inner j = i..rows

5
45
345
2345
12345   ← rows = 5

Unlike Program 5 (append 1..i while counting up), this counts the outer loop down and appends i..rows — total digits = n(n+1)/2.

How to Solve It

Outer loop counts down from rows; inner loop appends from the current start digit up to rows.

MethodIdeaBest for
Descending outeri = rows..1, then append i..rowsClassic reverse-ascending demos
CompactSame logic with fewer rows (e.g. 3)Quick dry-runs

Pseudocode

Pseudocode
for i from rows down to 1:
    line = ""
    for j from i to rows:
        append j
    print line

Cheat sheet

GoalPattern
Outer (rows)for (let i = rows; i >= 1; i--)
Inner (digits)for (let j = i; j <= rows; j++) line += j;
End the rowconsole.log(line);
Digits on first row1 (just rows)
Digits on last rowrows (1..rows)
Total digitsrows * (rows + 1) / 2

Printing Numbers vs Starting a New Line

APIEffectUse for
line += …Stays on the same rowEach digit
console.log(line)Ends the current rowAfter the inner loop finishes

Append without a newline, then end the row once.

Live Preview

Change the row count and the reverse ascending triangle updates instantly.

Whole numbers from 1 to 9 (single digits stay neat). Tap a chip or type a value — the preview redraws as you go.

Live result rows = 5 · 15 digits
5
45
345
2345
12345

JavaScript Programs

Three complete programs: fixed rows = 5, prompt input, and a compact rows = 3 demo. Use View Output for samples, or Try It Yourself to edit and run live.

Example 1 — Fixed rows = 5

Outer loop counts down; inner loop appends from i up to rows.

JavaScript
const rows = 5;

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

How It Works

1. Start at the peak. i = rows so the first row is just 5.

2. Append up to the fixed end. Inner j runs from i to rows — when i = 3, that builds 345.

3. End the row once. Call console.log(line) after the inner loop so digits stay on one line.

Example 2 — User Input Rows

Read rows with prompt and validate before drawing.

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

if (!Number.isFinite(rows) || rows < 1) {
  console.log("Please enter a positive integer.");
} else {
  for (let i = rows; i >= 1; i--) {
    let line = "";
    for (let j = i; j <= rows; j++) {
      line += j;
    }
    console.log(line);
  }
}
Try It Yourself

How It Works

1. Prompt and validate. Use parseInt and require rows >= 1.

2. Same two-loop core. Only the source of rows changes — descending outer / ascending inner match Example 1.

3. Entering 4. Ten digits total (4×5/2), last row 1234.

Example 3 — Compact rows = 3

Same descending-outer logic with fewer rows for a quick visual check.

JavaScript
const rows = 3;

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

How It Works

1. Shrink the size. Only rows changes — still i = rows..1 and j = i..rows.

2. Easier to trace. Six digits total — useful while learning the leftward growth.

3. Same end rule. The last row is always 1..rows.

Edge Cases & Pitfalls

Check these before calling the solution done.

Outer bound

Count i down from rows, not up from 1

Counting up with j = 1..i rebuilds Program 5 instead of this left-growing triangle.

Inner end

Keep the inner end fixed at rows

Ending at i would print one digit per row — no accumulation.

rows = 1

Single-row case

Output is just 1.

Bad input

Validate parseInt

Check Number.isFinite(rows) and require rows >= 1 before the loops.

Time and Space Complexity

ProgramTimeExtra space
Fixed / compact (Examples 1, 3)O(n²)O(n) per row string
User input (Example 2)O(n²)O(n) per row string

Total digits are 1 + 2 + … + n = n(n+1)/2 — still quadratic in n.

Key Takeaways

  • Rule: outer rows..1, inner append i..rows.
  • Growth: each new row adds one digit on the left.
  • Break the row: call console.log(line) only after the inner loop.
  • Complexity: O(n²) — triangular digit count.

One line: count the start digit down from n, append up to n each row, then log.

Frequently Asked Questions

The outer loop begins at i = rows. The inner loop appends j from i to rows, so the first row logs only that single digit.
When i = 1, the inner loop runs j from 1 to rows — appending 1, 2, 3, 4, 5 on one line.
Program 5 counts the outer loop up and logs 1..i. Program 6 counts the outer loop down and logs i..rows — digits grow from the left.
line += appends digits on the same row. console.log(line) ends the row after the inner loop finishes.
O(n²) for n rows because total appends are 1 + 2 + … + n = n(n+1)/2.
Yes — use line += j + " " and trimEnd before console.log if needed.
Use parseInt with Number.isFinite. Bare parseInt(prompt()) returns NaN on bad input.
One row prints a single digit 1 — the inner loop runs j from 1 to 1 once.

Did you know?

The outer loop starts from rows down to 1, and the inner loop appends i..rows — producing 5, 45, 345, and so on. Total appends still grow as O(n²).

Next: Reverse Row Number Triangle

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

Program 7 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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