Increasing odd-length number rows print consecutive digits 1..i with no spaces, where row lengths grow by 2: 1, 3, 5, 7, 9.
Remember
Rule: for odd i = 1, 3, 5, … maxN
print digits 1..i (no spaces)
1
123
12345
1234567
123456789 ← maxN = 9
The outer loop’s i += 2 is the whole trick — a clear follow-up after the jump triangle in Program 21.
Approach
How to Solve It
Walk odd lengths with i += 2; on each row append j from 1 to i with no spaces.
Method
Idea
Best for
Outer step 2
i += 2; inner 1..i
Learning, interviews
prompt input
Same loops; force odd max if needed
Interactive practice
Odd-only digits
Inner loop also uses j += 2
Variant practice
Pseudocode
Pseudocode
for i from 1 to maxN step 2:
line = ""
for j from 1 to i:
line += j
print line (with newline)
Cheat sheet
Goal
Pattern
Set max
const maxN = 9;
Outer loop
for (let i = 1; i <= maxN; i += 2)
Inner loop
for (let j = 1; j <= i; j++) line += j;
End row
console.log(line);
Force odd max
if (maxN % 2 === 0) maxN--;
Odd digits only
for (let j = 1; j <= i; j += 2)
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += j
Stays on the same row
Each digit
console.log(line)
Ends the line
After the inner loop
Build the row with += (no space), then break once with console.log. Putting console.log inside the inner loop prints one digit per line.
Try it
Live Preview
Change the maximum odd length and the pattern updates instantly. Even values are adjusted down by 1.
Whole numbers from 1 to 15. Even inputs become the next lower odd value.
Live resultmax = 9 · digits = 25
1
123
12345
1234567
123456789
Trace
Worked Walkthrough — maxN = 5
Trace each odd value of i and the digits concatenated on that row.
i
Inner j
Printed row
1
1
1
3
1 2 3
123
5
1 2 3 4 5
12345
Next odd i would be 7 — row length grows by exactly 2 each time.
Code
JavaScript Programs
Three complete programs: fixed maxN = 9, prompt input, and odd-only digits. Use View Output for sample results, or Try It Yourself to edit and run in the playground.
Example 1 — Fixed maxN = 9
Hard-coded maximum — outer i += 2, inner concatenates 1..i.
JavaScript
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);
}
1. Outer step.i runs 1, 3, 5, 7, 9 — those are the row lengths.
2. Inner digits. Append j from 1 to i with no space character.
3. End the row.console.log(line) prints the finished digit string and moves to the next line.
Example 2 — prompt Input
Read the maximum at runtime; if it is even, subtract 1 so the last row stays odd-length.
JavaScript
let maxN = parseInt(prompt("Enter the maximum value:"), 10);
if (!Number.isFinite(maxN) || maxN < 1) {
console.log("Please enter a positive integer.");
} else {
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);
}
}
1. Same outer loop. Row lengths are still 1, 3, 5, 7, 9.
2. Skip evens.j += 2 appends only 1, 3, 5, … up to i.
3. Compare shapes. Rows are shorter than Example 1 because half the digits are skipped.
Edge Cases & Pitfalls
Check these before calling the solution done.
i++
Forgot step 2
Using i++ prints every length 1, 2, 3, … — not only odd lengths. Keep i += 2.
spaces
Extra spaces
Use line += j, not line += j + " ", if you want tight digit strings like 12345.
log inside
Vertical digits
If console.log is inside the inner loop, each digit lands on its own line. Append with +=; log only after the loop.
even max
Even maximum
With maxN = 8, the last odd i is 7 — or call maxN-- first so intent is explicit.
maxN = 1
Single digit
Output is just 1 on one line.
NaN input
Validate parseInt
Letters or empty prompt yield NaN — check Number.isFinite(maxN) && maxN >= 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
Examples 1–2
O(n²)
O(n) for the current line string
Example 3
O(n²)
O(n) (fewer digits, same order)
Total digits for odd lengths = 1 + 3 + 5 + … + n ≈ (n/2)² → still O(n²).
Remember
Key Takeaways
Rule: for each odd i, concatenate digits 1..i with no spaces.
Step 2:i += 2 makes lengths 1, 3, 5, 7, 9.
Write vs log:line += j builds; console.log(line) breaks.
Complexity:O(n²) for maximum length n.
One line: step the outer loop by 2, and glue digits 1 through i together on each row.
Frequently Asked Questions
Because the outer loop uses i += 2, so i becomes 1, 3, 5, 7, 9 — each row prints one more odd count of digits than the previous.
line += j appends each digit immediately after the previous one — no space character is added.
Yes. Change the inner loop to for (let j = 1; j <= i; j += 2) to output 1, 13, 135, 1357, 13579 (see Example 3).
line += j stays on the same row while building digits. console.log(line) prints the completed row and adds a newline.
Because the outer loop runs while i <= 9. Change 9 to any odd maximum to extend the pattern.
Subtract 1 to make it odd (see Example 2) so the last row still has an odd length.
O(n²) for maximum row length n because total prints are 1+3+5+…+n.
Use parseInt(prompt(...), 10) and check Number.isFinite(maxN) && maxN >= 1 so bad input does not produce NaN.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.
🤔
Did you know?
Each 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.