JavaScript Odd Number Triangle Pattern (Left-Shifted)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
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.
Approach
How to Solve It
Walk odd starts from 1 to n; on each row, append odds from that start up to n.
Method
Idea
Best for
Step +2
Outer/inner both += 2 from odd starts
Learning, interviews
prompt input
Same loops; force odd n if needed
Interactive practice
Even mirror
Start both loops at 2
Variant 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
Goal
Pattern
Set max
const n = 10; (odds up to 9)
Outer loop
for (let i = 1; i <= n; i += 2)
Inner loop
for (let j = i; j <= n; j += 2) line += j;
End row
console.log(line);
Force odd
if (n % 2 === 0) n -= 1;
Even mirror
for (let i = 2; i <= n; i += 2)
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += j
Stays on the same row
Each odd digit
console.log(line)
Ends the line
After 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.
Try it
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 resultn = 9 · digits = 15
13579
3579
579
79
9
Trace
Worked Walkthrough — n = 5
Trace each odd outer start i and the odd digits appended up to n.
i
j values
Printed row
1
1, 3, 5
135
3
3, 5
35
5
5
5
As i jumps by 2, the row starts later — fewer odds, left-shifted look.
Code
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);
}
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);
}
}
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.