Shape Rule
Odd row lengths
Row 1 prints 1, row 2 prints 123, row 3 prints 12345, and so on — digits run together with no spaces.

The increasing odd-length number rows pattern uses i += 2 in the outer loop so each row prints 1..i with lengths 1, 3, 5, 7, 9 — a natural step after the jump triangle in Program 21. This tutorial covers the shape rule, step-size logic, a live preview, algorithm steps, worked JavaScript examples, edge cases, and complexity.
Odd row lengths
Row 1 prints 1, row 2 prints 123, row 3 prints 12345, and so on — digits run together with no spaces.
i += 2
for (let i = 1; i <= max_n; i += 2) walks odd values 1, 3, 5, 7, 9 as row lengths.
1..i
for (let j = 1; j <= i; j++) then line += j — no space between digits.
Same line / next line
Digits use line += j; end each row with console.log(line).
1–15 max
Pick an odd maximum and draw the odd-length rows pattern instantly in the browser.
Complexity
Total prints = 1+3+5+...+max_n; extra memory stays O(1).
An increasing odd-length number rows pattern prints consecutive digits 1..i on each row, with row lengths growing by 2 each time. With max_n = 9, the output is 1, 123, 12345, 1234567, 123456789.
In JavaScript you use for (let i = 1; i <= max_n; i += 2) in the outer loop and line += j in the inner loop, then console.log(line) ends each row.
It introduces loop step sizes — a simple change to i += 2 creates a whole new family of patterns.
Row lengths are 1, 3, 5, 7, 9 — always odd.
line += j concatenates digits on one line.
Digits run together — 123 not 1 2 3.
Follow Program 21; continue to Program 23 (number & asterisk mirror) next.
In short: for each odd i up to max_n, print digits 1 through i with line += j, then call console.log(line).
Given a positive odd integer max_n, print increasing odd-length rows: for each odd i from 1 to max_n, print digits 1 through i concatenated on one line.
// max_n = 9 (conceptual shape)
// 1
// 123
// 12345
// 1234567
// 123456789
for (let i = 1; i <= maxN; i += 2) {
let line = "";
for (let j = 1; j <= i; j++) {
line += j;
}
console.log(line);
} | Item | Type | Description |
|---|---|---|
max_n | int | Maximum row length (typically odd, e.g. 9). |
i | int | Outer loop — odd values 1, 3, 5, … up to max_n. |
j | int | Inner loop — prints digits 1 through i. |
| Printed output | text | Row i has i concatenated digits — no spaces. |
for i from 1 to max_n step 2:
line = ""
for j from 1 to i:
append j to line
console.log(line) | Approach | Idea | Best for |
|---|---|---|
| Outer i += 2 | 1, 123, 12345, … | Learning and interviews |
| User-input max_n | max_n = parseInt(prompt(...)) | Flexible console programs |
| Inner j += 2 | 1, 13, 135, 1357, … | Odd-only digit rows |
| Goal | Pattern |
|---|---|
| Walk odd lengths | for (let i = 1; i <= max_n; i += 2) |
| Print digits | for (let j = 1; j <= i; j++) |
| Write digit | line += j |
| End the row | console.log(line) |
| Odd-only variant | for (let j = 1; j <= i; j += 2) |
| User input | max_n = parseInt(prompt(...)) |
Same odd-length rows — different ways to control max_n and inner loop step.
range step 2Row lengths 1, 3, 5, 7, 9
line += jConcatenate digits — no spaces
j step 2Print 1, 3, 5 only in Example 3
even max_nSubtract 1 if user enters an even maximum
Reach for this pattern when teaching loop step sizes and concatenated digit output inside nested loops.
Natural follow-up after Program 21 — introduces outer loop step i += 2.
Outer/inner bound practice with an immediate visual check.
Combine loops with prompt() for a flexible row count.
Compare Program 21 (jump triangle) and Program 23 (number & asterisk mirror) 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 an odd maximum between 1 and 15 and draw the odd-length rows pattern in the browser.
Three complete JavaScript programs — fixed maximum, user input, and odd-only digits variant. Click View Output to reveal sample results, or Try it Yourself to run the code live.
Print five rows of odd-length consecutive digits with outer i += 2.
max_n = 9Hard-coded maximum — ideal for first demos and screenshots.
const maxN = 9;
for (let i = 1; i <= maxN; i += 2) {
let line = "";
for (let j = 1; j <= i; j++) {
line += j;
}
console.log(line);
} When i = 1, the inner loop appends 1 once. When i = 3, j runs 1, 2, 3 — output 123. When i = 9, digits 1 through 9 concatenate into 123456789. console.log(line) after the inner loop starts the next row.
Read the maximum with prompt() instead of hard-coding 9.
Read max_n with prompt() and parseInt(); adjust to odd if the user enters an even value.
const maxInput = prompt("Enter the maximum value:");
let maxN = parseInt(maxInput, 10);
if (maxN % 2 === 0) {
maxN--;
}
for (let i = 1; i <= maxN; i += 2) {
let line = "";
for (let j = 1; j <= i; j++) {
line += j;
}
console.log(line);
} Same nested-loop core as Example 1; only the source of max_n changes. The if (maxN % 2 === 0) maxN-- guard keeps the last row odd-length. Non-numeric input yields NaN with bare parseInt(prompt()) — validate with Number.isFinite for safer labs.
Use j += 2 in the inner loop to print only odd digits.
j += 2Keep max_n = 9 but print 1, 3, 5, 7, 9 instead of 1..i on each row.
const maxN = 9;
for (let i = 1; i <= maxN; i += 2) {
let line = "";
for (let j = 1; j <= i; j += 2) {
line += j;
}
console.log(line);
} Change only the inner loop to for (let j = 1; j <= i; j += 2) — the outer loop and console.log(line) logic stay the same. Each row prints odd digits up to i instead of every digit from 1 to i.
print is built in; use prompt() when reading input. Set max_n and loop variables i, j.
for (let i = 1; i <= max_n; i += 2) — row lengths are 1, 3, 5, 7, 9.
for (let j = 1; j <= i; j++) then line += j — digits concatenate with no spaces.
console.log(line) ends the row so the next outer iteration starts fresh.
Total prints grow as 1+3+5+...+max_n — O(n²) time, O(1) extra memory.
max_n = 9Trace each outer-loop value of i and the digits printed on each row.
i | Inner j range | Digits printed | Row output |
|---|---|---|---|
1 | 1 | 1 | 1 |
3 | 1..3 | 1, 2, 3 | 123 |
5 | 1..5 | 1, 2, 3, 4, 5 | 12345 |
7 | 1..7 | 1..7 | 1234567 |
9 | 1..9 | 1..9 | 123456789 |
Total digit prints: 1 + 3 + 5 + 7 + 9 = 25.
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: use (i + j) % 2 for row+column parity grids.
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 still → 55.
Pair the pattern with Number.isFinite checks around parseInt(prompt()) and positive-max checks.
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 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: learn i += 2 in the outer loop first; compare with i += 2 odd-digit variant in Example 3.
Small habits that keep number-pattern code clean.
Use line += j without spaces — not line += j + " " unless you want gaps.
prompt()Check parseInt(prompt()) with Number.isFinite so bad input does not leave max_n as NaN.
console.log(line) OutsideOnly call console.log(line) after the inner loop finishes the row.
Write each odd i and the j range before coding.
Trace max_n = 7 on paper before coding larger demos.
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 odd-length row patterns.
Each digit lands on its own line — you get a column, not a triangle.
→ Use line += j; console.log(line) only after the inner loop.
Using step 1 (no step-2 range) prints every length 1, 2, 3, 4 — not odd lengths only.
→ Use for (let i = 1; i <= max_n; i += 2) for odd row lengths.
line += j + " " produces 1 2 3 instead of 123.
→ Use line += j for concatenated digits.
Omitting console.log(line) glues every number onto one endless line.
→ Always end the row after the inner loop.
Letters or empty input yield NaN with bare parseInt(prompt()).
→ Validate with Number.isFinite and re-prompt on failure.
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.
max_n < 0Treat as invalid; re-prompt instead of silent empty output.
Output grows as n² characters — fine for labs, noisy for huge n.
parseInt(prompt()) yields NaN — validate with Number.isFinite first.
Subtract 1 to keep odd row lengths — see Example 2.
Try these variations to lock in the pattern.
line += j + " " for gaps1+3+5+...+max_n — O(n²) for maximum row length n.line += j stays on the line; console.log(line) advances — mix them carefully.max_n > 0 for interactive programs; max_n = 1 should print a single 1.Quick Takeaway: use i += 2 in the outer loop, line += j in the inner loop, then console.log(line) after each row.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(max²) | O(1) |
| Odd digits (Example 3) | O(max²) | O(1) |
The increasing odd-length number rows pattern is a compact lesson in loop step sizes: use i += 2 in the outer loop and line += j in the inner loop to concatenate digits. Master the fixed-max version, then try user input and the odd-only digit variant.
Practice the three examples above, then continue to Program 23 for the number & asterisk mirror pattern.
Use i += 2 for odd lengths — validate max_n and adjust even input when reading from the console.
for (let i = 1; i <= max_n; i += 2) in the outer loopline += j — no space between digitsmax_n with max_n -= 1 for user inputparseInt(prompt()) with Number.isFinite before using max_nconsole.log(line) inside the inner digit loop1 when you want odd lengths onlymax_n = 1 edge casePrint the pattern the beginner-friendly way.
i += 2 rows
Definitionline += j no space
Code1, 3, 5, 7, 9
ShapeOdd digits variant
VariantO(n²) time
AnalysisEach row length increases by 2 because the outer loop uses i += 2 (1, 3, 5, 7, 9). The inner loop appends 1..i with line += j — total prints grow as O(n²) for maximum row length n.
Move on to the number & asterisk mirror pattern in the JavaScript number-pattern series.
12 people found this page helpful