JavaScript Number Triangle Pattern (Increasing Jump)

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

What Is This Pattern?

An increasing jump number triangle starts each row at i, then jumps forward with a step m that shrinks after every print — jumps get tighter toward the end of the row.

Remember
Rule: row i starts with i
      then add shrinking step m

1
2 6
3 7 10
4 8 11 13
5 9 12 14 15     ← rows = 5, m starts at 4

Unlike the continuous counter in Program 20, each row resets its start to i and uses a fresh decreasing step.

How to Solve It

Print i first; set m = rows − 1 and k = i + m; then append k while shrinking m.

MethodIdeaBest for
Decreasing stepStart at i; jump with m--Learning, interviews
prompt inputSame loops; m = rows − 1Interactive practice
Custom stepFixed initial m instead of rows − 1Variant practice

Pseudocode

Pseudocode
for i from 1 to rows:
    line = i + " "
    m = rows - 1
    k = i + m
    for j from 1 to i - 1:
        line += k + " "
        m = m - 1
        k = k + m
    print line (with newline)

Cheat sheet

GoalPattern
Set rowsconst rows = 5;
Outer loopfor (let i = 1; i <= rows; i++)
Start rowlet line = i + " ";
Init steplet m = rows - 1; let k = i + m;
Jump loopfor (let j = 1; j < i; j++) { line += k + " "; m--; k = k + m; }
End rowconsole.log(line);
Custom steplet m = 3; instead of rows - 1

Printing Numbers vs Starting a New Line

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

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

Live Preview

Change the row count and the jump triangle updates instantly (m starts at rows − 1).

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

Live result rows = 5 · numbers = 15
1 
2 6 
3 7 10 
4 8 11 13 
5 9 12 14 15 

Worked Walkthrough — rows = 3

Trace each outer value of i, the starting step m = 2, and the jumps on that row.

iStartJumps (m shrinks)Printed row
11none1
22+2 → 42 4
33+2 → 5, then +1 → 63 5 6

Each row resets m to rows − 1; within the row, m-- makes later jumps smaller.

JavaScript Programs

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

Example 1 — Fixed rows = 5

Hard-coded height — each row starts at i, then jumps with m starting at 4.

JavaScript
const rows = 5;

for (let i = 1; i <= rows; i++) {
  let line = i + " ";
  let m = rows - 1;
  let k = i + m;

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

How It Works

1. Start the row. Append i first — that is always the leftmost value.

2. Set the jump. m = rows − 1 and k = i + m prepare the first jump value.

3. Shrink and advance. Append k, then m-- and k = k + m for each remaining slot; console.log(line) ends the row.

Example 2 — prompt Input

Read the row count at runtime; initial step scales as m = rows − 1.

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

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

    for (let j = 1; j < i; j++) {
      line += k + " ";
      m--;
      k = k + m;
    }
    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. Scale the step. m = rows − 1 grows with height, so taller triangles jump farther at first.

Example 3 — Custom Initial Step m = 3

Keep rows = 5 but start each row with m = 3 for tighter jumps than the default 4.

JavaScript
const rows = 5;

for (let i = 1; i <= rows; i++) {
  let line = i + " ";
  let m = 3;
  let k = i + m;

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

How It Works

1. Same structure. Outer loop and jump loop match Example 1.

2. Fixed seed. Only the initial m changes — m-- and k = k + m still apply.

3. Compare shapes. Row lengths stay the same; values climb more slowly than with m = rows − 1.

Edge Cases & Pitfalls

Check these before calling the solution done.

m outside

Step not reset

If m is declared outside the outer loop, later rows inherit a tiny step. Reset m at the start of every row.

order

Wrong update order

Append k, then m--, then k = k + m. Updating k before decreasing m uses the wrong step.

log inside

Vertical numbers

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

rows = 1

Single number

Output is just 1 (with a trailing space) — the jump loop never runs.

rows ≤ 0

Empty output

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

NaN input

Validate parseInt

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

Time and Space Complexity

ProgramTimeExtra space
Examples 1–3O(n²)O(n) for the current line string

Total numbers printed = 1 + 2 + … + n = n(n+1)/2 → O(n²).

Key Takeaways

  • Rule: row i starts with i, then jumps with a shrinking step m.
  • Reset m: set m = rows − 1 at the start of every row.
  • Write vs log: line += … + " " builds; console.log(line) breaks.
  • Complexity: O(n²) for n rows.

One line: print i, then keep adding a step that gets one smaller after every jump.

Frequently Asked Questions

Because the step variable m starts at rows − 1 and decreases after each appended number. Each next value adds the current m, so jumps shrink across the row.
m is the step size used to compute the next printed number in the row. It decreases each time, changing the increment between consecutive outputs.
Row 2 appends i = 2 first. Then m = 4 and k = i + m = 6 — the inner loop appends k once, giving 2 6.
line += k + " " stays on the same row with a trailing space. console.log(line) prints the completed row and adds a newline.
Total prints are 1+2+…+n = n(n+1)/2 — row i prints exactly i values.
Yes. Set m to a custom value instead of rows − 1 (see Example 3) — smaller steps produce tighter jumps.
O(n²) for n rows because total digit prints equal n(n+1)/2.
Use parseInt(prompt(...), 10) and check Number.isFinite(rows) && rows >= 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 starts at i, then adds a decreasing step m to compute the next value. As m shrinks after each append, the jumps get smaller toward the end of the row — total prints still equal n(n+1)/2 for n rows.

Next: Odd-Length Number Rows

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

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