Twin of Program 33 (i + j - 1 from 1): same growing shape, but both loops start at 0.
Approach
How to Solve It
Walk i from 0 to maxN; on each row append i + 1 values using i + j.
Method
Idea
Best for
Nested loops
Outer i = 0..maxN; inner j = 0..i; value i + j
Learning, interviews
prompt maxN
Same loops; read max index at runtime
Interactive practice
Compact maxN = 2
Same formula; easier to trace by hand
Paper walkthrough
Pseudocode
Pseudocode
for i from 0 to maxN:
line = ""
for j from 0 to i:
line += (i + j) + " "
print line (with newline)
Cheat sheet
Goal
Pattern
Set max index
const maxN = 5;
Outer loop
for (let i = 0; i <= maxN; i++)
Inner loop
for (let j = 0; j <= i; j++)
Value
line += (i + j) + " ";
End row
console.log(line);
Row length
i + 1 values
vs Program 33
i + j - 1 with i from 1
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += value + " "
Stays on the same row
Each 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 max index and the triangle from 0 updates instantly.
Whole numbers from 0 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultmaxN = 5 · rows = 6 · values = 21
0
1 2
2 3 4
3 4 5 6
4 5 6 7 8
5 6 7 8 9 10
Trace
Worked Walkthrough — maxN = 2
Trace each outer i, the formula i + j, and the printed row.
i
Values i+j
Printed row
0
0+0 = 0
0
1
1, 2
1 2
2
2, 3, 4
2 3 4
Each next row starts at i and adds one more consecutive value — total rows = maxN + 1.
Code
JavaScript Programs
Three complete programs: fixed maxN = 5, prompt max index, and compact maxN = 2. Use View Output for sample results, or Try It Yourself to edit and run in the playground.
Example 1 — Fixed maxN = 5
Hard-coded max index — zero-based nested loops with formula i + j.
JavaScript
const maxN = 5;
for (let i = 0; i <= maxN; i++) {
let line = "";
for (let j = 0; j <= i; j++) {
line += (i + j) + " ";
}
console.log(line);
}
1. Outer loop.i runs from 0 to 5 — six rows total.
2. Inner loop.j runs from 0 to i; append (i + j) + " ".
3. Newline.console.log(line) after the inner loop prints the row.
Example 2 — prompt Max Index
Read maxN at runtime; allow 0 as a valid minimum.
JavaScript
const maxN = parseInt(prompt("Enter max i:"), 10);
if (!Number.isFinite(maxN) || maxN < 0) {
console.log("Please enter a non-negative integer.");
} else {
for (let i = 0; i <= maxN; i++) {
let line = "";
for (let j = 0; j <= i; j++) {
line += (i + j) + " ";
}
console.log(line);
}
}
1. Same structure. Outer and inner loops match Example 1.
2. Only maxN changes. Three rows; total values = 1+2+3 = 6.
3. Trace first. Walk i = 0, 1, 2 on paper before scaling to 5 or more.
Edge Cases & Pitfalls
Check these before calling the solution done.
log inside
Vertical numbers
If console.log is inside the inner loop, each value lands on its own line. Log only after the row is built.
start at 1
Missing zero row
Starting i at 1 skips the 0 row and looks like Program 33 without the - 1.
j < i
Off-by-one length
Use j <= i so row i prints i + 1 values. j < i drops the last cell.
maxN = 0
Single zero
Output is just 0 — valid and useful for testing input handling.
no space
Glued digits
Omitting + " " prints 12 instead of 1 2.
NaN input
Validate parseInt
Letters or empty prompt yield NaN — check Number.isFinite(maxN) && maxN >= 0.
Analysis
Time and Space Complexity
Program
Time
Extra space
Examples 1–3
O(n²)
O(n) for the current line string
For max index n, rows = n + 1 and total values = 1 + 2 + … + (n+1) = (n+1)(n+2)/2 → O(n²).
Remember
Key Takeaways
Rule: print i + j for j = 0..i with i from 0 to maxN.
Zero-based: first cell is 0; row length is i + 1.
Write vs log:line += … builds; console.log(line) breaks.
Complexity:O(n²) for max index n.
One line: start both loops at 0 and print i + j to grow a triangle that begins with zero.
Frequently Asked Questions
Because the loops start at i = 0 and j = 0, so i + j = 0.
j increases from 0 to i, so i + j increases by 1 each step — producing consecutive numbers.
Program 33 uses i + j - 1 with i starting at 1. Program 34 uses i + j with i starting at 0.
Program 34 uses the formula i + j per cell. Program 35 is a right-aligned continuous counter triangle.
line += (i + j) + " " keeps values separated on the same row. console.log(line) ends the row.
Replace 5 with maxN in the outer loop bound — see Example 2.
O(n²) for max index n because total appends are 1 + 2 + ... + (n+1) when i runs 0..n.
Use parseInt with Number.isFinite so bad input does not produce NaN. Allow 0 as a valid max.
Only one row prints — a single 0.
🤔
Did you know?
Each 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.