Shape Rule
1..i digits on row i
Row 1 prints 1, row 2 prints 12, growing until row rows prints 1..rows.

The ascending number triangle pattern grows one digit per row: nested loops, building a line string vs console.log(line), and a clear visual result. This tutorial covers the shape rule, loop structure, a live preview, algorithm steps, worked JavaScript examples, edge cases, and complexity.
1..i digits on row i
Row 1 prints 1, row 2 prints 12, growing until row rows prints 1..rows.
Rows
for (let i = 1; i <= rows; i++) walks each line from one digit up to the full width.
Digits
for (let j = 1; j <= i; j++) appends digits 1 through i on that row.
Same line / next line
Digits use line += j; end each row with console.log(line).
1–20 rows
Pick a row count and draw the ascending number triangle instantly in the browser.
Complexity
Total digit prints = n(n+1)/2; extra memory stays O(1).
An ascending number triangle pattern starts with one digit on row 1 and grows by one digit each row. Each row prints consecutive digits from 1 up to i, expanding from top to bottom.
In JavaScript you solve it with two nested loops: the outer loop picks the row, the inner loop appends digits 1..i on that row, then console.log(line) moves to the next line.
It is a natural follow-up after Program 4’s left-aligned descending triangle. Once nested loops and line +=/console.log(line) click, pyramids, diamonds, and hollow shapes become much easier.
On row i, print digits 1 through i.
Outer counts up rows; inner prints digits 1..i.
line += j in the inner loop; console.log(line) after.
Natural step after Program 4; gateway to pyramid and hollow patterns.
In short: for each row i from 1 up to rows, append digits 1..i with line += j, then call console.log(line).
Given a positive integer rows, print an ascending number triangle: each row i shows digits 1 through i, with the outer loop counting from 1 up to rows.
// rows = 5
// 1
// 12
// 123
// 1234
// 12345 | Item | Type | Description |
|---|---|---|
rows | number | Number of triangle lines to print (typically ≥ 1). |
| Printed output | text | Each row prints 1..i; the first row has one digit, the last row has rows digits. |
for (let i = 1; i <= rows; i++) {
let line = "";
for (let j = 1; j <= i; j++) {
line += j;
}
console.log(line);
} | Approach | Idea | Best for |
|---|---|---|
| Nested loops | Outer rows + inner digits | Learning and interviews |
| Spaced output | line += j + " " | Easier reading per row |
| Goal | Pattern |
|---|---|
| Walk each row | for (let i = 1; i <= rows; i++) |
Print digits 1..i | for (let j = 1; j <= i; j++) { line += j; } |
| End the row | console.log(line); |
| Spaced digits | line += j + " "; |
| Program 1 contrast | for (let i = rows; i >= 1; i--) (descending outer) |
Same ascending number triangle — different ways to control rows and formatting.
i = 1..rowsCounts up each row — triangle grows
j = 1..iPrints ascending digits per row
line += j + " "Optional space between numbers on each row
parseInt(prompt())Validate row count when reading user input
Reach for this triangle when teaching or testing nested-loop basics.
Natural follow-up after Program 4 — same inner loop but the outer loop counts up instead of shrinking rows.
Outer/inner bound practice with an immediate visual check.
Combine loops with parseInt(prompt()) for a flexible row count.
Compare Program 1 (descending outer) and Program 6 (next in series) 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 3 and 9 and draw the ascending number triangle in the browser.
Three complete JavaScript programs — fixed rows, prompt() input, and a spaced-output variant. Click View Output to reveal sample console results, or Try it Yourself to run the code live.
Print five rows of the ascending number triangle with nested loops.
rows = 5Hard-coded height — ideal for first demos and screenshots.
const rows = 5;
for (let i = 1; i <= rows; i++) {
let line = "";
for (let j = 1; j <= i; j++) {
line += j;
}
console.log(line);
} When i = 1, the inner loop prints 1. When i = 5, it prints 12345 — each row adds one more digit. console.log(line) after the inner loop starts the next row.
Let the user choose the height at runtime.
Read rows with prompt() and validate the result.
const rowsInput = prompt("Enter the number of rows:");
const rows = parseInt(rowsInput, 10);
if (!Number.isFinite(rows) || rows < 1) {
console.log("Please enter a positive integer.");
} else {
for (let i = 1; i <= rows; i++) {
let line = "";
for (let j = 1; j <= i; j++) {
line += j;
}
console.log(line);
}
} Same inner-loop core as Example 1; only the source of rows changes from a literal to user input.
Add a space between digits for easier reading on each row.
Keep rows = 5 but print each digit followed by a space.
const rows = 5;
for (let i = 1; i <= rows; i++) {
let line = "";
for (let j = 1; j <= i; j++) {
line += j + " ";
}
console.log(line);
} Only the append changes — line += j + " " instead of line += j. Loop bounds stay the same as Example 1.
No imports needed. Set rows (fixed or from input).
for (let i = 1; i <= rows; i++) selects the current line, starting at one digit and growing.
for (let j = 1; j <= i; j++) appends digits 1..i with line += j.
console.log(line) ends the row so the next outer iteration starts fresh.
Total digit prints: 1+2+…+n = n(n+1)/2 — O(n²) time, O(1) extra memory.
rows = 5Trace each outer-loop value of i (counting up) and count how many digits the inner loop prints.
i | Inner j range | Printed row | Digits this row |
|---|---|---|---|
1 | 1..1 | 1 | 1 |
2 | 1..2 | 12 | 2 |
3 | 1..3 | 123 | 3 |
4 | 1..4 | 1234 | 4 |
5 | 1..5 | 12345 | 5 |
Total digit prints: 1 + 2 + 3 + 4 + 5 = 15 = 5×6/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: Program 4 shrinks each row from rows down to i.
Practice line += j 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 += j + " " for spaced digits on each row.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for n = 10 → 55.
Pair the pattern with Number.isFinite and positive-row checks after parseInt(prompt()).
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: trace i and j on paper for rows = 3 before coding — watch how each row grows by one digit.
Small habits that keep number-pattern code clean.
Use rows (or n) and keep i/j for row/column — or rename to row/col.
Number.isFiniteAvoid NaN when the user types letters instead of a number.
Only call console.log(line) after the inner loop finishes the row.
1..rows with j <= i matches “row i prints digits 1..i” naturally.
Trace rows = 3 on paper before coding larger demos.
Pro Tip: if the output is a vertical list of single digits, you almost certainly put console.log(line) inside the inner loop.
Mistakes that commonly break ascending number triangles.
Each digit lands on its own line — you get a column, not a triangle.
→ Use line += j for digits; console.log(line) only after the inner loop.
j <= rows prints a rectangle; wrong outer bounds flatten or invert the shape.
→ For this shape, keep j <= i.
Omitting console.log(line) glues every digit onto one endless line.
→ Always end the row after the inner loop.
parseInt(prompt())Letters or empty input yield NaN with bare parseInt(prompt()).
→ Check Number.isFinite(rows) and re-prompt on failure.
Switching to i = 0 without adjusting the inner bound prints an empty first row or wrong counts.
→ If 0-based, print digits 1..i+1 (e.g. j <= i + 1).
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²/2 characters — fine for labs, noisy for huge n.
Bare parseInt(prompt()) returns NaN — validate with Number.isFinite first.
Try line += j + " " for spaces between numbers.
Try these variations to lock in the pattern.
line += j + " " between digitsn(n+1)/2 — hence O(n²) time.line += j builds the row; console.log(line) advances — call log only after the inner loop.rows > 0 for interactive programs; rows = 1 should print a single 1.Quick Takeaway: outer loop picks the row, inner loop prints digits 1..i, then break the line — that is the whole pattern.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(rows²) | O(1) |
| Spaced output (Example 3) | O(rows²) | O(1) |
The ascending number triangle pattern is a small nested-loop exercise with lasting payoff: row/column thinking, line += vs console.log(line), and O(n²) intuition. Master the fixed-rows version, then try user input and spaced output.
Practice the three examples above, then continue to Program 6 for the next pattern in the series.
Row i prints 1..i — keep line += j for digits and console.log(line) for the break, and validate row counts when reading input.
for (let i = 1; i <= rows; i++) in the outer loopline += j for digits and console.log(line) after each rowrows ≥ 1 for interactive programsNumber.isFinite when reading user input with prompt()console.log(line) inside the inner digit looprows = 1 edge casePrint the triangle the beginner-friendly way.
Row i prints 1..i
DefinitionControls each row
CodeAppends digits with line += j
console.log(line) ends each row
O(n²) time
AnalysisRow i appends digits 1 through i. The outer loop counts up from 1 to rows, so each row grows by one digit — still O(n²) total logs.
Move on to the next pattern in the JavaScript number-pattern series.
11 people found this page helpful