Shape Rule
Continuous k++
Row 1 prints 1, row 2 prints 2 3, row 3 prints 4 5 6, and so on without restarting.

The continuous number triangle uses a running counter k so digits keep increasing across rows — a natural step after the fill-with-5 pattern in Program 19. This tutorial covers the shape rule, counter logic, a live preview, algorithm steps, worked JavaScript examples, edge cases, and complexity.
Continuous k++
Row 1 prints 1, row 2 prints 2 3, row 3 prints 4 5 6, and so on without restarting.
1..rows
for (let i = 1; i <= rows; i++) makes row i print i numbers.
k++ each append
line += k + " "; k++ appends k then increments — value carries to the next row.
Same line / next line
Numbers use line += k + " "; k++; end each row with console.log(line).
1–15 rows
Pick a row count and draw the continuous counter triangle instantly in the browser.
Complexity
Total prints = rows(rows+1)/2; extra memory stays O(1).
A continuous number triangle prints an ascending counter across rows — numbers never restart at 1 on each new line. With rows = 4, the output is 1, 2 3, 4 5 6, 7 8 9 10.
In JavaScript you declare k = 1 once, print k++ in the inner loop for i iterations per row, then console.log(line) ends each row.
It introduces a running counter variable — a key step after Program 19 and before jump-number patterns.
Declare k = 1 once before both loops.
Print then increment — sequence continues across rows.
line += k + " "; k++ in the inner loop; console.log(line) after.
Follow Program 19; continue to Program 21 (jump number triangle).
In short: set k = 1 once, for each row i print k++ for i numbers, then call console.log(line).
Given a positive integer rows, print a continuous number triangle: row i prints i numbers from a running counter k that starts at 1 and increments with k++ on every print.
// rows = 4 (conceptual shape)
// 1
// 2 3
// 4 5 6
// 7 8 9 10
let k = 1;
for (let i = 1; i <= rows; i++) {
let line = "";
for (let j = 1; j <= i; j++) {
line += k + " ";
k++;
}
console.log(line);
} | Item | Type | Description |
|---|---|---|
rows | int | Number of triangle lines to print (typically ≥ 1). |
k | int | Running counter — declared once, incremented each print. |
| Printed output | text | Row i has i spaced numbers — continuous sequence. |
let k = 1
for i from 1 to rows:
line = ""
for j from 1 to i:
append k + " " to line, then k++
console.log(line) | Approach | Idea | Best for |
|---|---|---|
| Running counter k++ | 1, 2 3, 4 5 6, … | Learning and interviews |
| User-input rows | parseInt(prompt(...)) | Flexible console programs |
| Custom start k | k = 10 before loops | Shift the whole sequence |
| Goal | Pattern |
|---|---|
| Walk each row | for (let i = 1; i <= rows; i++) |
| Init counter | k = 1 before both loops |
| Print and step | line += k + " "; k++ |
| End the row | console.log(line) |
| Custom start | k = 10 to shift sequence |
| User input | rows = parseInt(prompt(...)) |
Same continuous triangle — different ways to control the counter.
counterPrint k then increment — sequence continues
k = 1Before both loops — not inside outer loop
k = 10Shift start value in Example 3
no resetDo not reset k each row for continuous output
Reach for this pattern when teaching running counters and continuous sequences inside nested loops.
Natural follow-up after Program 19 — introduces a single running counter.
Outer/inner bound practice with an immediate visual check.
Combine loops with prompt() for a flexible row count.
Compare Program 19 (fill-with-5) and Program 21 (jump number triangle) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.
Choose a row count between 1 and 15 and draw the continuous number triangle in the browser.
Three complete JavaScript programs — fixed row count, user input, and custom start value for k. Click View Output to reveal sample results, or Try it Yourself to run the code live.
Print four rows of the continuous counter triangle with k++.
rows = 4Hard-coded height — ideal for first demos and screenshots.
const rows = 4;
let k = 1;
for (let i = 1; i <= rows; i++) {
let line = "";
for (let j = 1; j <= i; j++) {
line += k + " ";
k++;
}
console.log(line);
} When i = 1, k appends once as 1. When i = 2, k appends 2 then 3. When i = 4, k runs from 7 to 10 — the counter never resets. console.log(line) after the inner loop starts the next row.
Read the row count with prompt() instead of hard-coding 4.
Read rows with prompt() and parseInt(); k still starts at 1.
const rowsInput = prompt("Enter the number of rows:");
const rows = parseInt(rowsInput, 10);
let k = 1;
for (let i = 1; i <= rows; i++) {
let line = "";
for (let j = 1; j <= i; j++) {
line += k + " ";
k++;
}
console.log(line);
} Same nested-loop core as Example 1; only the source of rows changes. k is still declared once before the loops. Non-numeric input yields NaN with bare parseInt(prompt()) — validate with Number.isFinite for safer labs.
Start the counter from a value other than 1.
k = 10Shift the whole sequence by starting k at 10 instead of 1.
const rows = 4;
let k = 10;
for (let i = 1; i <= rows; i++) {
let line = "";
for (let j = 1; j <= i; j++) {
line += k + " ";
k++;
}
console.log(line);
} Change only the initial value of k — the inner loop and k++ logic stay the same. The sequence continues from 10 instead of 1.
console.log is built in; use prompt() when reading input. Set rows and let k = 1.
for (let i = 1; i <= rows; i++) makes row i print i numbers.
line += k + " "; k++ prints k then increments — value carries to the next row.
console.log(line) ends the row so the next outer iteration starts fresh.
Total prints: rows(rows+1)/2 — O(n²) time, O(1) extra memory.
rows = 4Trace each outer-loop value of i, the starting k, and the numbers printed on each row.
i | Start k | Numbers printed | Row output |
|---|---|---|---|
1 | 1 | 1 | 1 |
2 | 2 | 2, 3 | 2 3 |
3 | 4 | 4, 5, 6 | 4 5 6 |
4 | 7 | 7, 8, 9, 10 | 7 8 9 10 |
Total number prints: 1 + 2 + 3 + 4 = 10 = 4×5/2.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: change j <= i and watch the shape change.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: use (i + j) % 2 for row+column parity grids.
Practice line += k + " " vs console.log(line) without complex math.
Example: put console.log(line) inside the inner loop by mistake.
Swap digits for letters, stars, or spaced output once the loop works.
Example: use line += k + " " for spaced numbers on each row.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for n = 10 still → 55.
Pair the pattern with Number.isFinite checks around parseInt(prompt()) and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner JavaScript courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn k = 1 before the loops first; compare with custom start in Example 3.
Small habits that keep number-pattern code clean.
Declare k once before both loops — not inside the outer loop.
Number.isFiniteCheck parseInt(prompt()) with Number.isFinite so bad input does not leave rows as NaN.
Only call console.log(line) after the inner loop finishes the row.
Write row i, start k, and each k++ step before coding.
Trace rows = 4 on paper before coding larger demos.
Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put console.log(line) inside the inner loop.
Mistakes that commonly break continuous number patterns.
Each digit lands on its own line — you get a column, not a triangle.
→ Use line += k + " "; k++; console.log(line) only after the inner loop.
k resets each row and the sequence restarts at 1.
→ Assign k = 1 once before both loops.
Incrementing after the row newline (or skipping k++) skips or duplicates numbers.
→ Print k then k++ inside the inner loop.
Omitting console.log(line) glues every number onto one endless line.
→ Always end the row after the inner loop.
Letters or empty input yield NaN with bare parseInt(prompt()).
→ Validate with Number.isFinite and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 on one line.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Output grows as n² characters — fine for labs, noisy for huge n.
parseInt(prompt()) yields NaN — validate with Number.isFinite first.
Declaring k inside the outer loop restarts the sequence — not continuous.
Without k++, the same number prints repeatedly on each row.
Try these variations to lock in the pattern.
k inside outer looprows(rows+1)/2 — O(n²) for n rows.line += k + " "; k++ stays on the line; console.log(line) advances — mix them carefully.rows > 0 for interactive programs; rows = 1 should print a single 1.Quick Takeaway: declare k = 1 once, print k++ for i numbers per row, then break the line.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(rows²) | O(1) |
| Custom start (Example 3) | O(rows²) | O(1) |
The continuous number triangle is a compact lesson in running counters: declare k once, print k++ in the inner loop, and let the sequence continue across rows. Master the fixed-rows version, then try user input and a custom start value.
Practice the three examples above, then continue to Program 21 for the jump number triangle.
Keep k outside the outer loop — use line += k + " "; k++ inside the inner loop and validate rows when reading input.
k = 1 before both loopsline += k + " "; k++ in the inner looprows ≥ 1 for interactive programsparseInt(prompt()) with Number.isFinite before using rowsconsole.log(line) inside the inner digit loopk inside the outer loopk++ after each printrows = 1 edge casePrint the pattern the beginner-friendly way.
k++ continuous
DefinitionOnce before loops
CodePrint then increment
CodeRow i prints i nums
ShapeO(n²) time
AnalysisA single counter k starts at 1 and increments with k++ on every append — numbers continue across rows instead of restarting. Total prints still equal n(n+1)/2 for n rows.
Move on to the jump number triangle in the JavaScript number-pattern series.
12 people found this page helpful