JavaScript Number Rows Pattern (Odd Length)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

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.

How to Solve It

Walk odd lengths with i += 2; on each row append j from 1 to i with no spaces.

MethodIdeaBest for
Outer step 2i += 2; inner 1..iLearning, interviews
prompt inputSame loops; force odd max if neededInteractive practice
Odd-only digitsInner loop also uses j += 2Variant 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

GoalPattern
Set maxconst maxN = 9;
Outer loopfor (let i = 1; i <= maxN; i += 2)
Inner loopfor (let j = 1; j <= i; j++) line += j;
End rowconsole.log(line);
Force odd maxif (maxN % 2 === 0) maxN--;
Odd digits onlyfor (let j = 1; j <= i; j += 2)

Printing Numbers vs Starting a New Line

APIEffectUse for
line += jStays on the same rowEach digit
console.log(line)Ends the lineAfter 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.

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 result max = 9 · digits = 25
1
123
12345
1234567
123456789

Worked Walkthrough — maxN = 5

Trace each odd value of i and the digits concatenated on that row.

iInner jPrinted row
111
31 2 3123
51 2 3 4 512345

Next odd i would be 7 — row length grows by exactly 2 each time.

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);
}
Try It Yourself

How It Works

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);
  }
}
Try It Yourself

How It Works

1. Prompt and parse. Convert the answer with parseInt(..., 10).

2. Validate and oddify. Reject bad input; if even, maxN-- keeps an odd last length.

3. Same loops. Outer i += 2 and inner 1..i match Example 1.

Example 3 — Odd-Only Digits j += 2

Keep maxN = 9 but print only odd digits on each row.

JavaScript
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);
}
Try It Yourself

How It Works

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.

Time and Space Complexity

ProgramTimeExtra space
Examples 1–2O(n²)O(n) for the current line string
Example 3O(n²)O(n) (fewer digits, same order)

Total digits for odd lengths = 1 + 3 + 5 + … + n ≈ (n/2)² → still O(n²).

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.

Next: Number & Asterisk Mirror

Continue with the next pattern in the JavaScript number-pattern series.

Program 23 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful