Left-Shifted Odd Number Triangle Pattern in JavaScript

Beginner
⏱️ 8 min read
📚 Updated: Sep 2026
🎯 3 Code Examples
🚀 Live Preview
Step Size Loop

What You’ll Learn

The left-shifted odd number triangle prints consecutive odd digits on each row while the starting odd value increases — a great exercise in nested loops with step size 2. This tutorial covers the shape rule, loop structure, a live preview, algorithm steps, worked JavaScript examples, edge cases, and complexity.

Shape Rule

Odd numbers only

Row 1 prints 13579, row 2 prints 3579, row 3 prints 579, and so on as the start shifts right.

Outer Loop

i += 2 / j += 2

for (let i = 1; i <= maxN; i += 2) picks the starting odd number for each row: 1, 3, 5, 7, 9.

Inner Loop

step 2 to maxN

for (let j = i; j <= maxN; j += 2) appends odd digits from the row start up to maxN.

line += vs console.log()

Same line / next line

Odd digits use line += j; end each row with console.log(line).

Live Preview

1–20 max

Pick a maximum value and draw the left-shifted odd triangle instantly in the browser.

O(n²)

Complexity

Total digit appends shrink each row; complexity is still O(n²) for maximum n.

Introduction

A left-shifted odd number triangle prints consecutive odd numbers on each row while the starting odd value increases by 2. With maxN = 10, the output is 13579, 3579, 579, 79, 9.

In JavaScript you solve it with nested loops that step by 2: for (let i = 1; i <= maxN; i += 2) and for (let j = i; j <= maxN; j += 2), then console.log(line) ends each row.

Why it matters?

It teaches step-size loops (+= 2) before more complex parity-based patterns.

Key Highlights

Step Size 2

i += 2 / j += 2 and i += 2 / j += 2 visit only odd values.

Left Shift

Each row starts at a larger odd i, so fewer digits print.

Print Then Break

line += j in the inner loop; console.log(line) after.

Series Foundation

Follow Program 16; continue to Program 18 (alternating odd/even rows).

In short: for each odd start i from 1 to maxN, append odd j from i to maxN stepping by 2, then call console.log(line).

📝 Problem & Approach

Given a positive integer maxN, print a left-shifted odd number triangle: row starting at odd i appends odd digits from i to maxN stepping by 2.

JavaScript
# maxN = 10 (conceptual shape)
# 13579
# 3579
# 579
# 79
# 9
for (let i = 1; i <= maxN; i += 2)
    for (let j = i; j <= maxN; j += 2)
        line += j   # odd digits i..maxN
    console.log(line)                # next row

Inputs & Outputs

ItemTypeDescription
maxNintUpper bound for odd digits on each row (typically ≥ 1).
Printed outputtextEach row prints consecutive odd numbers from i to maxN.

Minimal workflow

Pseudocode
for i from 1 to maxN step 2:
    for j from i to maxN step 2:
        append j to line
    console.log(line)

Approach comparison

ApproachIdeaBest for
Nested loops + step 213579, 3579, …Learning and interviews
Even max adjustmentif (maxN % 2 === 0) maxN -= 1;User-input programs
Even-number variantStart at 2 with step 2Mirror pattern with evens

⚡ Quick Reference

GoalPattern
Walk each row startfor (let i = 1; i <= maxN; i += 2)
Append odd digitfor (let j = i; j <= maxN; j += 2) { line += j; }
End the rowconsole.log(line)
Force odd maximumif (maxN % 2 === 0) maxN -= 1;
Even variantfor (let i = 2; i <= maxN; i += 2) with matching inner step
Spaced outputline += j + " "

📋 Step 2 vs Even Max vs Even Variant

Same left-shift idea — different bounds and step handling.

range step 2
odd start

Outer loop picks 1, 3, 5, 7, 9

inner step 2
odd digits

Inner loop prints only odd values

maxN -= 1
even fix

Adjust even user input to odd bound

Learning tip
trace i,j

Trace maxN = 10 on paper before coding

Context

When This Pattern Shows Up

Reach for this pattern when teaching loop step sizes and shrinking row widths.

  1. First lab exercise

    Step-size loops build on binary patterns from Programs 15 and 16.

  2. Nested-loop warm-up

    Outer/inner bound practice with an immediate visual check.

  3. Console I/O practice

    Combine loops with prompt() for a flexible row count.

  4. Gateway to variants

    Compare Program 16 (binary triangle) and Program 18 (alternating odd/even rows) next.

  5. Not a UI layout tool

    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.

🔮 Live Preview

Choose a maximum between 1 and 20 and draw the left-shifted odd triangle in the browser.

Try 9, 10, or 15. Even values are adjusted down to the nearest odd number.

Live result
Press "Draw pattern".

Examples Gallery

Three complete JavaScript programs — fixed maximum, user input, and an even-number mirror variant. Click View Output to reveal sample results, or Try it Yourself to run the code live.

📚 Getting Started

Print the left-shifted odd triangle with maxN = 10 and += 2 loops.

Example 1 — Fixed maxN = 10

Hard-coded upper bound — ideal for first demos and screenshots.

JavaScript
const maxN = 10;

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

How It Works

When i = 1, the inner loop prints 1, 3, 5, 7, 9 as 13579. When i = 5, it prints 5, 7, 9 as 579, and so on as the start shifts right. console.log(line) after the inner loop starts the next row.

📈 User Input

Read the maximum with prompt() and adjust even input to an odd bound.

Example 2 — User Input Version

Read maxN with prompt() and parseInt() (check Number.isFinite in real apps); subtract 1 when the value is even.

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

if (maxN % 2 === 0) {
  maxN -= 1;
}

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

How It Works

maxN = 8 becomes 7 after the even adjustment, so the last odd printed is 7. The nested step-2 loops stay the same as Example 1. Non-numeric input yields NaN with bare parseInt(prompt()) — validate with Number.isFinite for safer labs.

⚡ Even Variant

Mirror the pattern with even numbers starting at 2 instead of 1.

Example 3 — Even Number Mirror

Start both loops at 2 and step by 2 to append only even digits up to maxN = 10.

JavaScript
const maxN = 10;

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

How It Works

Same left-shift structure as Example 1, but both loops start at 2 and visit only even values. Compare with the odd version to see how the start value changes the sequence.

🧠 How the Algorithm Prints Rows

1

Set up

console.log is built in; use prompt() when reading input. Set maxN (fixed or from input).

Setup
2

Outer loop (row start)

for (let i = 1; i <= maxN; i += 2) picks the starting odd number: 1, 3, 5, 7, 9.

Row
3

Inner loop (odd digits)

for (let j = i; j <= maxN; j += 2) appends each odd value with line += j.

Odd-only
4

New line

console.log(line) ends the row so the next outer iteration starts fresh.

Break
=

Left-shifted triangle complete

Each row prints fewer digits as i grows — O(n²) time for maximum n, O(1) extra memory.

🔎 Worked Walkthrough — maxN = 10

Trace each outer-loop value of i and note the odd j values printed on each row.

iInner j valuesPrinted row
11, 3, 5, 7, 913579
33, 5, 7, 93579
55, 7, 9579
77, 979
999

Five rows for maxN = 10; digit count shrinks from 5 down to 1.

Use Cases

Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.

1. Teaching Nested Loops

Clearest visual proof that outer and inner bounds interact.

Example: change j <= i and watch the shape change.

2. Pattern Series Base

Foundation for inverted, pyramid, diamond, and hollow variants.

Example: use (i + j) % 2 for row+column parity grids.

3. Console Formatting Drills

Practice line += digit vs row newline without complex math.

Example: put console.log(line) inside the inner loop by mistake.

4. Character Substitution

Swap digits for letters, stars, or spaced output once the loop works.

Example: use line += j + " " for spaced digits on each row.

5. Complexity Intuition

Triangular totals make O(n²) concrete for beginners.

Example: count printed digits for n = 10 still → 55.

6. Input Validation Labs

Pair the pattern with Number.isFinite after parseInt(prompt(), 10) and positive-bound checks.

Example: reject maxN <= 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.

Advantages

Why this pattern earns a permanent spot in beginner JavaScript courses.

  1. 1. Instant Visual Feedback

    Wrong bounds show up immediately as a broken staircase.

  2. 2. Minimal Concepts

    Only loops and console output — no arrays or math libraries.

  3. 3. Easy to Extend

    Invert, center, hollow, or change the fill character with small edits.

  4. 4. O(1) Extra Memory

    Streaming output needs no storage beyond loop counters.

Pro Tip: learn step 2 first; compare with step 1 to see how the step size changes which numbers print.

Usage Tips

Small habits that keep number-pattern code clean.

  1. 1. Name Bounds Clearly

    Use maxN (avoid shadowing builtin max) and keep i/j for row/column — or rename to start/value.

  2. 2. Validate with Number.isFinite

    Check Number.isFinite(maxN) after parseInt(prompt(), 10) so bad input does not leave maxN unset.

  3. 3. Keep console.log Outside

    Only call console.log(line) after the inner loop finishes the row.

  4. 4. Force Odd Maximum

    if (maxN % 2 === 0) maxN -= 1; keeps the bound odd when reading input.

  5. 5. Dry-Run One Small n

    Trace maxN = 10 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.

Common Pitfalls

Mistakes that commonly break left-shifted odd number patterns.

  1. 1. Newline Inside the Inner Loop

    Each digit lands on its own line — you get a column, not a triangle.

    → Use line += j for digits; console.log(line) only after the inner loop.

  2. 2. Using Step 1 Instead of Step 2

    for (let j = i; j <= maxN; j++) (step 1) prints even numbers too — the row no longer contains only odds.

    → For odd-only rows, keep for (let j = i; j <= maxN; j += 2).

  3. 3. Forgetting the Row Break

    Omitting console.log(line) glues every digit onto one endless line.

    → Always end the row after the inner loop.

  4. 4. Bare parseInt(prompt())

    Letters or empty input return NaN with bare parseInt(prompt(), 10).

    → Check Number.isFinite(maxN) and re-prompt on failure.

  5. 5. Off-by-One on 0-Based Loops

    Switching to a 0-based outer start without adjusting the stop value can drop the last row or print wrong odds.

    → Prefer for (let i = 1; i <= maxN; i += 2) with for (let j = i; j <= maxN; j += 2) for the digits.

Edge Cases

Check these inputs before calling the solution done.

maxN = 1

Single digit

Output is just 1 on one line.

maxN = 0

Empty pattern

Outer loop never runs — print nothing or show a message.

Negative

maxN < 0

Treat as invalid; re-prompt instead of silent empty output.

Large n

Many rows

Output grows as n²/2 characters — fine for labs, noisy for huge n.

Bad input

Non-numeric input

parseInt(prompt(), 10) returns NaN on bad input — validate with Number.isFinite first.

Even max

Even maximum input

Subtract 1 or prompt again — otherwise the last odd may not match intent.

step 1

Wrong step size

Step 1 includes even numbers — use i += 2 / j += 2 for odd-only output.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Ascending binary triangle

  • Inner loop j = 1 to i with j % 2
  • Continue with Program 16

2. Alternating odd/even rows

  • Switch start value by row parity
  • Continue with Program 18

3. Even mirror pattern

  • Start at 2 with step 2 on both loops
  • Compare output with Example 3

4. Spaced odd output

  • Use line += j + " " between digits
  • Harder follow-up after this page

Notes

  • Shrinking rows. Digit count per row decreases as i grows — still O(n²) prints for maximum n.
  • line += j stays on the line; console.log(line) advances — mix them carefully.
  • Validate maxN > 0 for interactive programs; maxN = 1 should print a single 1.
  • This page is left-aligned. Centered pyramids need leading spaces — covered later in the series.

Quick Takeaway: outer loop steps by 2 for row starts, inner loop appends odd j up to maxN, then break the line.

⏱️ Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–2)O(max²)O(1)
Even variant (Example 3)O(max²)O(1)
Wrap Up

🎉 Conclusion

The left-shifted odd number triangle is a compact lesson in loop step sizes: += 2 on both loops prints only odd values while each row starts later. Master the fixed-max version, then try the user-input and even-mirror variants.

Practice the three examples above, then continue to Program 18 for the alternating odd/even number triangle.

Use for (let i = 1; i <= maxN; i += 2) and for (let j = i; j <= maxN; j += 2) for odd-only digits — keep line += j for numbers and console.log(line) for the break, and adjust even maxN when reading input.

💡 Best Practices

✅ Do

  • Explain outer/inner i += 2 / j += 2 before coding
  • Use line += j for digits and console.log(line) after each row
  • Validate maxN ≥ 1 for interactive programs
  • Check Number.isFinite(maxN) after parseInt(prompt(), 10) before using maxN
  • State O(n²) time when asked about complexity

❌ Don’t

  • Call console.log(line) inside the inner digit loop
  • Use step 1 when you meant odd-only with i += 2 / j += 2
  • Skip the newline after each row
  • Ignore bad console input in user-facing demos
  • Skip the maxN = 1 edge case

Key Takeaways

Knowledge Unlocked

Five things to remember about this left-shifted pattern

Print the pattern the beginner-friendly way.

5
Core concepts
02

Outer loop

i += 2 / j += 2 picks start

Code
% 03

Inner loop

Inner step 2 to maxN

Code
04

Left shift

Rows shrink each line

Shape
O 05

Complexity

O(n²) time

Analysis

❓ 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.
On the last iteration, i = 9, so the inner loop prints only 9 once.
line += j stays on the same row while building digits. console.log(line) prints the completed row and adds a newline. Digits use += inside the inner loop; the row break uses console.log after the inner loop.
Not with step 2 starting from odd numbers — that visits only odd values. To include 10, use step 1 or handle even values separately (see Example 3).
Subtract 1 (if maxN % 2 === 0) maxN -= 1 so the last printed odd matches the intended width, as shown in Example 2.
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 with Number.isFinite and a maxN > 0 check after prompt(), or validate the raw string before converting 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.

Continue to Program 18

Move on to the alternating odd/even number triangle 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