A continuous number triangle (Floyd’s triangle) prints a running counter across rows — row i has i numbers, and the counter never resets.
Remember
Rule: k starts at 1 once
row i prints i values of k++
1
2 3
4 5 6
7 8 9 10 ← rows = 4
Declaring k before the loops is the whole trick — a natural step after the fill pattern in Program 19.
Approach
How to Solve It
Set k = 1 once; for each row i, append k++ exactly i times, then end the line.
Method
Idea
Best for
Running counter
k++ inside the inner loop
Learning, interviews
prompt input
Same loops; read row count at runtime
Interactive practice
Custom start
Initialize k to any start value
Variant practice
Pseudocode
Pseudocode
k = 1
for i from 1 to rows:
line = ""
for j from 1 to i:
line += k + " "
k = k + 1
print line (with newline)
Cheat sheet
Goal
Pattern
Set rows
const rows = 4;
Init counter
let k = 1; before both loops
Outer loop
for (let i = 1; i <= rows; i++)
Inner loop
for (let j = 1; j <= i; j++) { line += k + " "; k++; }
End row
console.log(line);
Custom start
let k = 10; before the loops
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += k + " "
Stays on the same row
Each spaced number
console.log(line)
Ends the line
After the inner loop
Build the row with +=, then break once with console.log. Putting console.log inside the inner loop prints one number per line.
Try it
Live Preview
Change the row count and the continuous triangle updates instantly.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 4 · numbers = 10
1
2 3
4 5 6
7 8 9 10
Trace
Worked Walkthrough — rows = 3
Trace each outer value of i, the starting k, and the numbers printed on that row.
i
Start k
Prints
Printed row
1
1
1×
1
2
2
2×
2 3
3
4
3×
4 5 6
After three rows, k is 7 — ready for a fourth row if you add one.
Code
JavaScript Programs
Three complete programs: fixed rows = 4, prompt input, and a custom start for k. Use View Output for sample results, or Try It Yourself to edit and run in the playground.
Example 1 — Fixed rows = 4
Hard-coded row count — k starts at 1 and climbs across every row.
JavaScript
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);
}
1. Init once.let k = 1 sits before both loops so the counter survives row changes.
2. Outer loop.i runs from 1 to rows — row i needs i numbers.
3. Inner loop. Append k + " ", then k++; after i times, console.log(line).
Example 2 — prompt Input
Read the row count at runtime; k still starts at 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 {
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);
}
}
1. Prompt and parse. Convert the answer with parseInt(..., 10).
2. Validate first. Reject NaN or non-positive values before looping.
3. Same loops. Counter logic matches Example 1 once rows is known.
Example 3 — Custom Start k = 10
Shift the whole sequence by starting k at 10 instead of 1.
JavaScript
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);
}
1. Same structure. Outer and inner loops match Example 1.
2. Different seed. Only the initial k changes — shape and row lengths stay the same.
3. Compare shapes. You still get 1 + 2 + 3 + 4 = 10 numbers; they just start at 10.
Edge Cases & Pitfalls
Check these before calling the solution done.
k inside
Counter resets
If let k = 1 is inside the outer loop, every row restarts at 1. Keep k outside both loops.
log inside
Vertical numbers
If console.log is inside the inner loop, each number lands on its own line. Append with +=; log only after the inner loop.
forgot ++
Repeated digits
Skipping k++ prints the same number repeatedly. Increment after every append.
rows = 1
Single number
Output is just 1 (with a trailing space) on one line.
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.
Analysis
Time and Space Complexity
Program
Time
Extra space
Examples 1–3
O(n²)
O(n) for the current line string
Total numbers printed = 1 + 2 + … + n = n(n+1)/2 → O(n²).
Remember
Key Takeaways
Rule: row i prints i values from a shared counter k++.
Init once: declare k before the loops so it never resets.
Write vs log:line += k + " " builds; console.log(line) breaks.
Complexity:O(n²) for n rows.
One line: keep one counter outside the loops, print i values of k++ on row i, then break the line.
Frequently Asked Questions
Because k is assigned once (let k = 1) before the loops and incremented with k++ every time a number is appended — its value carries over to the next row.
Row 1 prints 1, so k becomes 2. Row 2 appends k twice: 2, then 3 after k++.
k resets each row and the sequence restarts at 1 — you get a growing 1, 1 2, 1 2 3 triangle, not a continuous counter.
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.
Yes. Set k = 10 (or any value) before the loops — the sequence continues from that start (see Example 3).
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?
A single counter k starts at 1 and increments with k++ on every append — numbers continue across rows instead of restarting. Total prints equal n(n+1)/2 for n rows.