Unlike Program 42 (hollow square), this pattern grows one extra number each row — total numbers for n rows is n(n+1)/2.
Approach
How to Solve It
One outer row loop, leading spaces for indent, then an ascending number loop. End each row with console.log.
Method
Idea
Best for
Right-aligned
" ".repeat(rows - i), then 1..i
Classic triangle demos
Left-aligned
Skip leading spaces; append 1..i only
Simpler first dry-runs
Pseudocode
Pseudocode
for i from 1 to rows:
line = spaces of length (rows - i)
for k from 1 to i:
append k and a space
print line
Cheat sheet
Goal
Pattern
Outer (rows)
for (let i = 1; i <= rows; i++)
Leading spaces
let line = " ".repeat(rows - i);
Append 1..i
for (let k = 1; k <= i; k++) line += k + " ";
End the row
console.log(line);
Total numbers
rows * (rows + 1) / 2
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += …
Stays on the same row
Spaces and each number
console.log(line)
Ends the current row
After the row is fully built
Append without a newline, then end the row once.
Try it
Live Preview
Change the row count and the right-aligned triangle updates instantly.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 15 numbers
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Trace
Worked Walkthrough
Trace spaces and the ascending sequence when rows = 4.
Row i
Spaces
Numbers
Printed row
1
3
1
1
2
2
1 2
1 2
3
1
1 2 3
1 2 3
4
0
1 2 3 4
1 2 3 4
Space count is rows - i. Total numbers for n rows = n(n+1)/2.
Code
JavaScript Programs
Three complete programs: fixed rows = 5, prompt input, and a left-aligned contrast. Use View Output for samples, or Try It Yourself to edit and run live.
Example 1 — Fixed rows = 5
Hard-coded height — leading spaces, then ascending 1..i on each row.
JavaScript
const rows = 5;
for (let i = 1; i <= rows; i++) {
let line = " ".repeat(rows - i);
for (let k = 1; k <= i; k++) {
line += k + " ";
}
console.log(line);
}
1. Walk each row. Outer loop i runs from 1 to rows.
2. Push right with spaces." ".repeat(rows - i) adds more indent on shorter rows.
3. Append 1..i. Inner loop appends each number plus a space, then console.log(line) ends the row.
Example 2 — User Input Rows
Read the row count with prompt and validate before drawing.
JavaScript
const rows = parseInt(prompt("Enter number of rows:"), 10);
if (!Number.isFinite(rows) || rows < 1) {
console.log("Please enter a positive integer.");
} else {
for (let i = 1; i <= rows; i++) {
let line = " ".repeat(rows - i);
for (let k = 1; k <= i; k++) {
line += k + " ";
}
console.log(line);
}
}