Shape Rule
Right-aligned triangle
Row i prints numbers 1 to i, with leading spaces before the digits.

The right-aligned number triangle prints 1, then 1 2, then 1 2 3, … — a natural follow-up after Program 42’s hollow square border. This tutorial covers leading-space indentation, ascending sequences, nested loops, a live preview, worked JavaScript examples, edge cases, and complexity.
Right-aligned triangle
Row i prints numbers 1 to i, with leading spaces before the digits.
i = 1..rows
for (let i = 1; i <= rows; i++) — ascending outer loop, one row per iteration.
rows..i+1
" ".repeat(rows - i) — adds leading spaces for right alignment.
line +=
for (let k = 1; k <= i; k++) then line += k + " ".
3–9 rows
Pick a row count and draw the right-aligned number triangle in the browser.
Complexity
Total prints = n(n+1)/2 — work scales as n².
A right-aligned number triangle prints numbers from 1 to i on each row: 1, then 1 2, then 1 2 3, and so on. With rows = 5, shorter rows shift right thanks to a leading-space loop.
In JavaScript you build each row with line += " ".repeat(rows - i) for leading spaces, then line += k + " " for k = 1..i, then console.log(line).
It combines a space loop with an ascending number loop — a key step after Program 42’s hollow grid pattern.
Ascending sequence.
rows - i spaces.
Readable column spacing.
Follow Program 42; continue to Program 44 next.
In short: outer i = 1..rows, leading spaces " ".repeat(rows - i), numbers k = 1..i with line += k + " ", then console.log(line).
Given rows = 5, print a right-aligned ascending triangle: leading spaces while j > i, then numbers from 1 to i.
// rows = 5
// 1
// 1 2
// 1 2 3
// 1 2 3 4
//1 2 3 4 5 | Item | Type | Description |
|---|---|---|
rows | number | Triangle height — also controls leading-space count. |
i | number | Outer loop — current row number (1 to rows). |
k | number | Number loop — prints digits 1..i. |
for i from 1 to rows:
for j from rows down to i+1: print one space
for k from 1 to i: print k with trailing space
print newline | Approach | Idea | Best for |
|---|---|---|
| Fixed rows | 1, 1 2, … | Learning and interviews |
| User-input rows | parseInt(prompt(), 10) | Configurable triangle size |
| Left-aligned variant | Remove space loop | Contrast with right alignment |
| Goal | Pattern |
|---|---|
| Outer loop | for (let i = 1; i <= rows; i++) |
| Leading spaces | line += " ".repeat(rows - i) |
| Number loop | for (let k = 1; k <= i; k++) |
| Append number | line += k + " " |
| End the row | console.log(line) |
| Program 42 contrast | Hollow square grid — not an ascending triangle |
Same ascending triangle — different ways to control rows and alignment.
i = 1..rowsOne row per iteration
rows - iLeading-space indent
k = 1..iAscending sequence
skip space loopFlush-left triangle
Reach for this pattern when teaching dual inner loops, ascending sequences, and right-aligned console output.
Natural follow-up — moves from a 2D grid with border conditions to a triangle with leading spaces and ascending digits.
Practice separating space printing from number printing before tackling more complex shapes.
Combine loops with prompt() for flexible row counts.
Compare Program 42 (hollow square) and Program 44 (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 dual inner loops, formatted output, and O(n²) thinking.
Choose a row count between 3 and 9 and draw the right-aligned number triangle in the browser.
Three complete JavaScript programs — fixed rows, prompt() input, and left-aligned contrast. Click View Output to reveal sample console results, or Try it Yourself to run the code live.
Print five rows of the right-aligned number triangle with space and number loops.
rows = 5Hard-coded row count — ideal for first demos and screenshots.
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);
} When i = 1, the space loop prints four spaces, then 1. When i = 5, no leading spaces — output 1 2 3 4 5.
Read the row count with prompt() instead of hard-coding 5.
Read rows with prompt() and validate rows > 0 (check with Number.isFinite in real apps).
const rowsInput = prompt("Enter the number of rows:");
const rows = parseInt(rowsInput, 10);
if (!Number.isFinite(rows) || rows <= 0) {
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);
}
} Same space-and-number loop core as Example 1; only rows comes from user input instead of being hard-coded as 5.
Remove the space loop to see how right alignment changes the shape.
Same ascending sequence without leading spaces — numbers start flush left.
const rows = 5;
for (let i = 1; i <= rows; i++) {
let line = "";
for (let k = 1; k <= i; k++) {
line += k + " ";
}
console.log(line);
} Only the space loop is removed — the number loop stays the same. Compare this flush-left output with Example 1 to see what the space loop contributes.
No imports needed. Set rows = 5 and loop variables i, k.
for (let i = 1; i <= rows; i++) — ascending outer loop; one row per iteration.
line += " ".repeat(rows - i) — adds leading spaces for right alignment.
for (let k = 1; k <= i; k++) then line += k + " " — ascending sequence.
console.log(line) ends the row after both inner steps finish.
Total numbers = n(n+1)/2 — O(n²) time, O(1) extra memory.
rows = 5, row i = 3Trace row 3 — space count, numbers printed, and full row output.
| Step | Detail | Output so far |
|---|---|---|
| Space loop | " ".repeat(2) — two spaces | |
k = 1 | line += "1 " | 1 |
k = 2 | line += "2 " | 1 2 |
k = 3 | line += "3 " | 1 2 3 |
| Newline | End row 3 | 1 2 3 |
Space count per row = rows - i. Numbers per row = i. Total prints = n(n+1)/2 for n rows.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: remove the space loop and watch the triangle snap left.
Foundation for right-aligned variants with separate space and number loops.
Example: compare with Program 42 (hollow square) and Program 44 next.
Practice line += spacing and row newlines without complex math.
Example: put console.log(line) inside the number loop by mistake.
Add leading spaces once the three-loop structure works.
Example: loop k from i down to 1 for a descending row variant.
Triangular totals make O(n²) concrete for beginners.
Example: count printed numbers for rows = 5 — total is 1+2+3+4+5 = 15.
Pair the pattern with Number.isFinite and prompt() validation.
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, the space loop, and the number loop on paper for rows = 3 before coding — watch how the space count shrinks each row.
Small habits that keep number-pattern code clean.
Leading spaces (" ".repeat(rows - i)) and number loop (k = 1..i) must run in order before console.log(line).
Number.isFiniteUse Number.isFinite(rows) so bad prompt() input does not crash when converting rows.
console.log Outside the Inner LoopOnly call console.log(line) after the inner loop finishes the row.
Write the ascending sequence 1..i on paper before coding the number loop.
Trace i = 1..3 on paper before coding the full rows = 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 right-aligned number triangles.
Each digit lands on its own line — you get a column, not a triangle.
→ Use line += k + " "; console.log(line) only after both inner steps.
Without " ".repeat(rows - i), every row starts at the left margin.
→ Run the space loop before the number loop on every row.
Combining spaces and numbers in a single inner loop is harder to read and debug.
→ Keep separate space and number loops — see Examples 1 and 3.
Tab characters produce inconsistent alignment across consoles.
→ Use line += " ".repeat(rows - i) for leading spaces.
parseInt(prompt())Letters or empty input yield NaN with bare parseInt(prompt()).
→ Check Number.isFinite(rows) and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 on one line — no leading spaces when rows = 1.
Outer loop never runs when rows < 1 — print nothing or show a message.
rows < 1Treat as invalid; re-prompt instead of silent empty output.
Two rows: 1 and 1 2.
Bare parseInt(prompt()) yields NaN on bad input — use Number.isFinite first.
Total numbers = rows(rows+1)/2 — grows quadratically with rows.
Try these variations to lock in the pattern.
i prints 1 to irows - iNumber.isFinite(rows) after parseInt(prompt())i = 1..rows. Leading spaces " ".repeat(rows - i). Number loop k = 1..i appends with line += k + " ".line +=, then console.log(line) once per row.rows ≥ 1 for interactive programs; rows = 1 prints a single 1.rows - i — compare with Example 3 where removing the space loop gives a left-aligned triangle.Quick Takeaway: outer i = 1..rows, leading spaces " ".repeat(rows - i), numbers k = 1..i with line += k + " ", 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 right-aligned number triangle is a compact lesson in dual inner loops and formatted output: append spaces with " ".repeat(rows - i), append 1..i with line += k + " ", and end each row with console.log(line). Master the fixed-rows version, then try user input and the left-aligned contrast.
Practice the three examples above, then continue to Program 44 for the next pattern in the series.
Leading spaces create right alignment — keep space and number loops separate and validate rows when reading input.
for (let i = 1; i <= rows; i++) in the outer loopline += " ".repeat(rows - i)for (let k = 1; k <= i; k++) line += k + " "rows ≥ 1 for interactive programsNumber.isFinite(rows) after parseInt(prompt())console.log(line) inside the number looprows = 1 edge casePrint the pattern the beginner-friendly way.
1..i per row
DefinitionRows i = 1..rows
Coderows - i spaces
Alignk = 1..i
CodeO(n²) time
AnalysisEach row appends numbers 1 to i. Leading spaces use " ".repeat(rows - i) before the number loop; line += k + " " keeps columns readable in the output.
Move on to the next pattern in the JavaScript number-pattern series.
12 people found this page helpful