JavaScript Descending Number Triangle Pattern (Odd Length)

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

What Is This Pattern?

An odd-length descending triangle prints ascending digits 1..i only for odd row widths — 1234567, 12345, 123, 1.

Remember
Rule: for i = n down to 1 step −2
        print 1..i

1234567
12345
123
1         ← n = 7 (odd only)

The outer step of −2 skips even widths like 6 and 4 — unlike Program 13, which uses every length and flips direction.

How to Solve It

Walk odd widths from n down; on each row, print digits 1 through i.

MethodIdeaBest for
Step −2Outer i -= 2; inner prints 1..iLearning, interviews
prompt inputSame loops; force odd n if neededInteractive practice
Step −1Outer i-- includes every widthContrast / full triangle

Pseudocode

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

Cheat sheet

GoalPattern
Set max widthconst n = 7; (prefer odd)
Outer loopfor (let i = n; i >= 1; i -= 2)
Inner loopfor (let j = 1; j <= i; j++) line += j;
End rowconsole.log(line);
Force oddif (n % 2 === 0) n -= 1;
All widthsfor (let i = n; i >= 1; i--)

Printing Numbers vs Starting a New Line

APIEffectUse for
line += jStays on the same rowEach sequential 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 width n — even values are forced odd so only odd-length rows appear.

Whole numbers from 1 to 9. Even n becomes n − 1 so the top row stays odd-length.

Live result n = 7 · digits = 16
1234567
12345
123
1

Worked Walkthrough — n = 5

Trace each odd outer value of i and the ascending digits printed.

iDigits 1..iPrinted row
51 2 3 4 512345
31 2 3123
111

i -= 2 jumps from 5 → 3 → 1, so even widths never appear.

JavaScript Programs

Three complete programs: fixed n = 7, prompt with odd enforcement, and a step-by-1 contrast. Use View Output for sample results, or Try It Yourself to edit and run in the playground.

Example 1 — Fixed n = 7

Hard-coded max width — outer steps by −2; inner prints 1..i.

JavaScript
const n = 7;

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

How It Works

1. Outer loop. i visits 7, 5, 3, 1 because of i -= 2.

2. Inner loop. j runs from 1 to i, appending each digit with line += j.

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

Example 2 — prompt Input

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

JavaScript
let n = parseInt(prompt("Enter an odd maximum (e.g., 9):"), 10);

if (!Number.isFinite(n) || n < 1) {
  console.log("Please enter a positive integer.");
} else {
  if (n % 2 === 0) {
    n -= 1;
  }
  for (let i = n; i >= 1; 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. Force odd. If n is even, subtract 1 so the top row stays odd-length (6 → 5).

3. Same loops. Outer still steps by −2; only the source of n changes.

Example 3 — Step by 1 (i--)

Same inner loop, but include every width — even-length rows appear for comparison.

JavaScript
const n = 7;

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

How It Works

1. Change the step. Using i-- instead of i -= 2 visits every width from n to 1.

2. Same inner loop. Each row still prints ascending digits 1..i.

3. Compare shapes. Even rows like 123456 now appear — showing why the step of −2 mattered.

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.

i--

Even rows appear

Using i-- instead of i -= 2 prints every width — that is Example 3, not the odd-only pattern.

even n

Force odd

Starting at even n with i -= 2 still works but begins on an even width. Subtract 1 first for a clean odd-only 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–2 (step −2)O(n²)O(n) for the current line string
Step by 1 (Example 3)O(n²)O(n) for the current line string

Odd-only digits: 1 + 3 + … + n ≈ n²/4 → still O(n²). Full step-by-1 is n(n+1)/2.

Key Takeaways

  • Rule: outer i = n..1 step −2; print 1..i on each row.
  • Vs Program 13: always ascending digits; skip even widths instead of flipping direction.
  • Write vs log: line += j builds; console.log(line) breaks.
  • Complexity: O(n²) for max width n.

One line: step the row width down by 2, and print ascending digits up to that width.

Frequently Asked Questions

The outer loop uses for (let i = n; i >= 1; i -= 2), so it visits only odd widths: 7, 5, 3, 1. Even lengths like 6, 4, 2 are skipped.
Because the step of −2 skips even row lengths. After 1234567 (7 digits), the next row is 5 digits (12345), not 6.
line += j stays on the same row while building digits. console.log(line) prints the completed row and adds a newline.
Program 13 alternates ascending/descending direction with i % 2. Program 14 always prints 1..i but only for odd row lengths using a step of −2.
O(n²) where n is the maximum row width. You print about 1+3+5+…+n odd-width digits, which is still O(n²).
Subtract 1 to make it odd (if (n % 2 === 0) n -= 1) so the first row stays odd-length.
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?

Only odd-length rows print. The outer loop uses i -= 2 (7, 5, 3, 1) and the inner loop prints 1..i — still O(n²) total digit prints for maximum width n.

Next: Alternating Binary Triangle

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

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