JavaScript Odd Number Triangle Pattern (Left-Shifted)

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

What Is This Pattern?

A left-shifted odd triangle prints consecutive odd digits on each row while the starting odd value increases — 13579, 3579, 579, …

Remember
Rule: for i = 1 to n step 2
        for j = i to n step 2
          print j

13579
3579
579
79
9         ← n = 9 (or 10)

Both loops use += 2 from an odd start, so even values never appear — a clean follow-up after Program 16’s binary bits.

How to Solve It

Walk odd starts from 1 to n; on each row, append odds from that start up to n.

MethodIdeaBest for
Step +2Outer/inner both += 2 from odd startsLearning, interviews
prompt inputSame loops; force odd n if neededInteractive practice
Even mirrorStart both loops at 2Variant practice

Pseudocode

Pseudocode
for i from 1 to n step 2:
    line = ""
    for j from i to n step 2:
        line += j
    print line (with newline)

Cheat sheet

GoalPattern
Set maxconst n = 10; (odds up to 9)
Outer loopfor (let i = 1; i <= n; i += 2)
Inner loopfor (let j = i; j <= n; j += 2) line += j;
End rowconsole.log(line);
Force oddif (n % 2 === 0) n -= 1;
Even mirrorfor (let i = 2; i <= n; i += 2)

Printing Numbers vs Starting a New Line

APIEffectUse for
line += jStays on the same rowEach odd digit
console.log(line)Ends the lineAfter the inner loop

Build the row with +=, then break once with console.log. Putting console.log inside the inner loop prints one digit per line.

Live Preview

Change max n — even values are forced odd so the last digit stays odd.

Whole numbers from 1 to 15. Even n becomes n − 1 for a clean odd bound.

Live result n = 9 · digits = 15
13579
3579
579
79
9

Worked Walkthrough — n = 5

Trace each odd outer start i and the odd digits appended up to n.

ij valuesPrinted row
11, 3, 5135
33, 535
555

As i jumps by 2, the row starts later — fewer odds, left-shifted look.

JavaScript Programs

Three complete programs: fixed n = 10, prompt with odd enforcement, and an even-number mirror. Use View Output for sample results, or Try It Yourself to edit and run in the playground.

Example 1 — Fixed n = 10

Hard-coded max — both loops step by 2 from odd starts (odds up to 9).

JavaScript
const n = 10;

for (let i = 1; i <= n; i += 2) {
  let line = "";
  for (let j = i; j <= n; j += 2) {
    line += j;
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Outer loop. i visits 1, 3, 5, 7, 9 — the starting odd for each row.

2. Inner loop. j runs from i to n stepping by 2, appending each odd digit.

3. Newline. console.log(line) after the inner loop prints the row and starts the next one.

Example 2 — prompt Input

Read max at runtime; force it odd if the user enters an even number.

JavaScript
let n = parseInt(prompt("Enter the maximum value:"), 10);

if (!Number.isFinite(n) || n < 1) {
  console.log("Please enter a positive integer.");
} else {
  if (n % 2 === 0) {
    n -= 1;
  }
  for (let i = 1; i <= n; i += 2) {
    let line = "";
    for (let j = i; j <= n; j += 2) {
      line += j;
    }
    console.log(line);
  }
}
Try It Yourself

How It Works

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

2. Force odd. If n is even, subtract 1 so the last odd matches a clean bound (8 → 7).

3. Same loops. Outer and inner still step by 2 — only the source of n changes.

Example 3 — Even Number Mirror

Same left-shift structure, but both loops start at 2 for even digits only.

JavaScript
const n = 10;

for (let i = 2; i <= n; i += 2) {
  let line = "";
  for (let j = i; j <= n; j += 2) {
    line += j;
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Even starts. i and j begin at 2 instead of 1.

2. Same step. += 2 still skips every other value — now only evens.

3. Compare shapes. Same left-shift idea as Example 1; only the parity of the sequence changes.

Edge Cases & Pitfalls

Check these before calling the solution done.

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 inner loop.

step 1

All integers

Using i++ / j++ instead of += 2 prints every integer, not odds only.

start 2

Even mirror

Starting at 2 yields the even variant (Example 3), not the odd triangle.

n = 1

Single digit

Output is just 1 on one line.

n ≤ 0

Empty output

The outer loop never runs. Guard prompt input with n >= 1.

NaN input

Validate parseInt

Letters or empty prompt yield NaN — check Number.isFinite(n) && n >= 1.

Time and Space Complexity

ProgramTimeExtra space
Examples 1–3O(n²)O(n) for the current line string

About k(k+1)/2 digits where k ≈ n/2 odd (or even) values → still O(n²).

Key Takeaways

  • Rule: outer i = 1..n step 2; inner j = i..n step 2.
  • Left shift: each row starts at a larger odd, so fewer digits print.
  • Write vs log: line += j builds; console.log(line) breaks.
  • Complexity: O(n²) for max n.

One line: step odd starts upward, and on each row print odds from that start to n.

Frequently Asked Questions

Both loops increment by 2 starting from an odd value, so they visit only odd values: 1, 3, 5, 7, 9.
Each next row starts from a larger odd number (i increases by 2), so fewer digits print on that row.
line += j stays on the same row while building digits. console.log(line) prints the completed row and adds a newline.
Subtract 1 (if n % 2 === 0) n -= 1 so the last printed odd matches a clean odd bound, as shown in Example 2.
Yes. Start both loops at 2 with step 2 (see Example 3) to get 246810, 46810, …
O(n²) for maximum value n. Only about half the numbers are visited due to step size 2, but nested loops still dominate.
Use parseInt(prompt(...), 10) and check Number.isFinite(n) && n >= 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?

Both loops step by 2 with j += 2, so only odd numbers print. Each row starts at a larger odd value, so the triangle shifts left — still O(n²) for maximum n.

Next: Alternating Odd/Even Triangle

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

Program 18 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