JavaScript Number Triangle Pattern (Increasing Jump)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
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.
Approach
How to Solve It
Print i first; set m = rows − 1 and k = i + m; then append k while shrinking m.
Method
Idea
Best for
Decreasing step
Start at i; jump with m--
Learning, interviews
prompt input
Same loops; m = rows − 1
Interactive practice
Custom step
Fixed initial m instead of rows − 1
Variant 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
Goal
Pattern
Set rows
const rows = 5;
Outer loop
for (let i = 1; i <= rows; i++)
Start row
let line = i + " ";
Init step
let m = rows - 1; let k = i + m;
Jump loop
for (let j = 1; j < i; j++) { line += k + " "; m--; k = k + m; }
End row
console.log(line);
Custom step
let m = 3; instead of rows - 1
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += … + " "
Stays on the same row
Each spaced number
console.log(line)
Ends the line
After 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.
Try it
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 resultrows = 5 · numbers = 15
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
Trace
Worked Walkthrough — rows = 3
Trace each outer value of i, the starting step m = 2, and the jumps on that row.
i
Start
Jumps (m shrinks)
Printed row
1
1
none
1
2
2
+2 → 4
2 4
3
3
+2 → 5, then +1 → 6
3 5 6
Each row resets m to rows − 1; within the row, m-- makes later jumps smaller.
Code
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);
}
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);
}
}
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);
}
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.