Shape Rule
Left-shifted triangle
Row i appends i + 1 numbers computed as i + j.

The increasing number triangle using i + j prints 0, 1 2, 2 3 4, … — a natural follow-up after Program 33’s i + j - 1 triangle starting from 1. This tutorial covers the i + j formula, nested loops, a live preview, worked JavaScript examples, edge cases, and complexity.
Left-shifted triangle
Row i appends i + 1 numbers computed as i + j.
i = 0..max_n
for (let i = 0; i <= max_n; i++) — zero-based outer loop, one growing row per iteration.
0..i
for (let j = 0; j <= i; j++) — appends i + 1 values per row.
i + j
Each value is i + j — row i starts at i when j = 0.
2–9 max_n
Pick a max_n value and draw the zero-based increasing triangle in the browser.
Complexity
Prints per row = i — total work scales as n².
A left-shifted increasing number triangle prints values from the formula i + j on each row. With max_n = 5, you get 0, 1 2, 2 3 4, and so on.
In JavaScript you use nested loops: outer i = 0..max_n, inner j = 0..i, appending (i + j) with a trailing space.
It combines zero-based nested loops with a compact formula — a step after Program 33’s i + j - 1 pattern.
Formula for each value.
Zero-based grow.
When i=0, j=0 → 0.
Follow Program 33; continue to Program 35 (right-aligned counter) next.
In short: outer loop i = 0..max_n, inner j = 0..i, append i + j with a space, then console.log(line).
Given max_n = 5, print a zero-based left-shifted increasing triangle: for each row i, print j = 0..i values of i + j separated by spaces.
// max_n = 5 (i runs 0..5)
const max_n = 5;
for (let i = 0; i <= max_n; i++) {
let line = "";
for (let j = 0; j <= i; j++) {
line += (i + j) + " ";
}
console.log(line);
} | Item | Type | Description |
|---|---|---|
max_n | int | Maximum outer-loop value — rows run from i = 0 to i = max. |
i | int | Outer loop — current row index (starts at 0). |
j | int | Inner loop — column index; runs 0..i per row. |
for i from 0 to max_n:
for j from 0 to i:
print (i + j) + space
print newline | Approach | Idea | Best for |
|---|---|---|
| Fixed formula | 0, 1 2, … | Learning and interviews |
| User-input max_n | parseInt(prompt(...), 10) | Configurable triangle size |
| Compact trace | max_n = 2 on paper first | Debugging loop bounds |
| Goal | Pattern |
|---|---|
| Outer loop | for (let i = 0; i <= max_n; i++) |
| Inner loop | for (let j = 0; j <= i; j++) |
| Print value | line += (i + j) + " " |
| End the row | console.log(line) |
| User input | parseInt(prompt(...), 10) |
Same increasing triangle — different ways to control the max_n row index.
i = 0..max_nZero-based outer loop
i + jStarts at 0
j = 0..ii + 1 values per row
j = 0 → iRow starts at row number
Reach for this pattern when teaching formula-based output, growing inner loops, and arithmetic in nested loops.
Natural follow-up after i + j - 1 — introduces zero-based loops with i + j.
Outer/inner bound practice with an immediate visual check.
Combine loops with ReadLine for a flexible row count.
Compare Program 33 (i + j - 1) and Program 35 (right-aligned counter) 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 max_n value between 2 and 9 and draw the increasing triangle in the browser.
Three complete JavaScript programs — fixed max_n, user input, and a smaller trace demo. Click View Output to reveal sample console results, or Try it Yourself to run the code live.
Print six rows (i = 0..5) of the increasing triangle with the i + j formula.
max_n = 5Hard-coded maximum row index — ideal for first demos and screenshots.
const max_n = 5;
for (let i = 0; i <= max_n; i++) {
let line = "";
for (let j = 0; j <= i; j++) {
line += (i + j) + " ";
}
console.log(line);
} When i = 0, the inner loop prints 0+0 = 0. When i = 4, it prints 4, 5, 6, 7, 8 — output 4 5 6 7 8.
Read the max_n row index with prompt() and parseInt() instead of hard-coding 5.
Read max_n with prompt() and parseInt() instead of hard-coding 5.
const maxInput = prompt("Enter max i:");
const max_n = parseInt(maxInput, 10);
if (!Number.isFinite(max_n) || max_n < 0) {
console.log("Please enter a non-negative integer.");
} else {
for (let i = 0; i <= max_n; i++) {
let line = "";
for (let j = 0; j <= i; j++) {
line += (i + j) + " ";
}
console.log(line);
}
} Same formula core as Example 1; only max_n comes from user input instead of being hard-coded as 5. Non-numeric input yields NaN with bare parseInt(prompt(), 10) — validate with Number.isFinite for safer labs.
Run with max_n = 2 to trace every row on paper before scaling up.
max_n = 2Same nested-loop formula with a smaller max_n for quick tracing.
const max_n = 2;
for (let i = 0; i <= max_n; i++) {
let line = "";
for (let j = 0; j <= i; j++) {
line += (i + j) + " ";
}
console.log(line);
} Only max_n changes from 5 to 2 — the nested-loop formula stays identical. Trace i = 0, 1, 2 on paper to see how each row adds one more value.
No imports needed for fixed max_n; use prompt() when reading. Set const max_n = 5 and loop variables i, j.
for (let i = 0; i <= max_n; i++) — zero-based outer loop, one growing row per iteration.
for (let j = 0; j <= i; j++) — appends i + 1 values per row.
line += (i + j) + " " — each value from the arithmetic formula.
console.log(line) ends the row after the inner loop finishes.
Prints per row = i + 1 — O(n²) time, O(1) extra memory.
max_n = 5Trace each outer-loop value of i, inner-loop range, values printed, and full row output.
i | Inner range (j) | Values (i+j) | Row output |
|---|---|---|---|
0 | 0 | 0 | 0 |
1 | 0, 1 | 1, 2 | 1 2 |
2 | 0, 1, 2 | 2, 3, 4 | 2 3 4 |
3 | 0..3 | 3, 4, 5, 6 | 3 4 5 6 |
4 | 0..4 | 4, 5, 6, 7, 8 | 4 5 6 7 8 |
5 | 0..5 | 5, 6, 7, 8, 9, 10 | 5 6 7 8 9 10 |
Prints per row = i + 1 — total prints = (max_n+1)(max_n+2)/2 when i runs 0..max_n.
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 inner bound to j <= max_n and watch every row print the same width.
Foundation for formula-based triangles and left-shifted sequences starting at 1.
Example: continue to Program 35 for a right-aligned continuous counter triangle.
Practice line += vs row newline without complex math.
Example: put console.log(line) inside the inner loop by mistake.
Add spaces between digits once the two-loop structure works.
Example: use line += (i + j) + " " between digits for wider spacing.
Triangular totals make O(n²) concrete for beginners.
Example: count printed numbers for max_n = 5 — total is 1+2+3+4+5+6 = 21.
Pair the pattern with Number.isFinite and non-negative max_n validation.
Example: reject max_n < 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 stdio 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 max_n = 2 before coding — watch how row i starts at i when j = 0.
Small habits that keep number-pattern code clean.
Outer bound must be i <= max_n starting at i = 0 — row count is max + 1.
Number.isFiniteAvoid undefined behavior when the user types letters instead of a number.
Only call console.log(line) after the inner loop finishes the row.
Write the formula for each (i, j) pair before coding the loops.
Trace i = 0..2 on paper before coding the full max_n = 5 demo.
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 increasing number triangles.
Each digit lands on its own line — you get a column, not a triangle.
→ Use line += (i + j) + " "; console.log(line) only after the inner loop.
Using i + j - 1 or starting at i = 1 shifts the triangle — it no longer starts at 0.
→ Keep i + j with i = 0..max_n and j = 0..i.
j <= max_n prints a rectangle — every row has the same width.
→ Keep for (let j = 0; j <= i; j++) so row i prints i + 1 values.
Printing numbers without a space makes multi-digit values run together on wider rows.
→ Append a space after each number: line += (i + j) + " ".
parseInt(prompt())Letters or empty input yield NaN with bare parseInt(prompt(), 10).
→ Validate with Number.isFinite and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 0 — one value, one row.
Outer loop never runs when max_n < 0 — print nothing or show a message.
max_n < 0Treat as invalid; re-prompt instead of silent empty output.
Two rows: 0 and 1 2.
Bare parseInt(prompt(), 10) yields NaN on bad input — validate with Number.isFinite first.
Total prints = (max_n+1)(max_n+2)/2 — grows quadratically with max_n.
Try these variations to lock in the pattern.
i + j - 1 with i starting at 1j = 0, i + j = imax_n >= 0 after reading inputi + j. Inner loop runs j = 0..i — row i prints i + 1 numbers.line += (i + j) + " " stays on the line; console.log(line) advances — mix them carefully.max_n >= 0 for interactive programs; max_n = 0 prints a single 0.j = 0, the value is always i — compare with Program 33 where the formula is i + j - 1.Quick Takeaway: outer loop i = 0..max_n, inner j = 0..i, print i + j, then console.log(line).
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Smaller demo (Example 3) | O(n²) | O(1) |
The increasing number triangle starting from 0 is a compact lesson in zero-based nested loops: compute each value with i + j, grow the inner bound to i, and end each row with console.log(line). Master the fixed-max_n version, then try user input and a smaller trace demo.
Practice the three examples above, then continue to Program 35 for the right-aligned continuous counter triangle.
Outer loop must start at i = 0 — validate max_n when reading with prompt().
for (let i = 0; i <= max_n; i++) in the outer loopfor (let j = 0; j <= i; j++) prints i + 1 valuesline += (i + j) + " "parseInt(prompt(), 10) with Number.isFiniteconsole.log(line) inside the inner loopi = 1 (skips the zero row)j <= max_n in the inner loop (prints a rectangle)max_n = 0 edge casePrint the pattern the beginner-friendly way.
i + j
Definitioni = 0..max_n
Codej=0 → i
Codeconsole.log(line) after the inner loop
O(n²) time
AnalysisEach printed value is computed as i + j. With i = 0 and j = 0 the first row prints 0; row i = 2 prints 2, 3, 4 — a zero-based left-shifted increasing triangle.
Move on to the right-aligned continuous counter triangle in the JavaScript number-pattern series.
12 people found this page helpful