Shape Rule
only odd row widths
Row lengths are 7, 5, 3, 1 — each row prints 1 through i with no even-width lines.

The odd-length descending number triangle teaches how a custom outer-loop step (i -= 2) skips even row widths. This tutorial covers the shape rule, loop structure, a live preview, algorithm steps, worked JavaScript examples, edge cases, and complexity.
only odd row widths
Row lengths are 7, 5, 3, 1 — each row prints 1 through i with no even-width lines.
Rows
for (let i = maxN; i >= 1; i -= 2) visits only odd row lengths from the top down.
1..i ascending
for (let j = 1; j <= i; j++) prints digits 1 through i on every row.
Same line / next line
Digits use line += j; end each row with console.log(line).
1–20 max
Pick an odd maximum and draw the odd-length descending triangle instantly in the browser.
Complexity
Total digit prints still = n(n+1)/2; extra memory stays O(1).
An odd-length descending number triangle prints ascending digits on each row, but only for odd row widths. With maxN = 7, the output is 1234567, 12345, 123, 1.
In JavaScript you solve it with a descending outer loop that steps by 2: for (let i = maxN; i >= 1; i -= 2), an inner loop for (let j = 1; j <= i; j++) that appends each digit, then console.log(line) ends each row.
It shows how changing the loop step creates entirely new shapes — not just different bounds.
i -= 2 skips even row widths.
Inner loop always prints 1..i on every row.
line += j in the inner loop; console.log(line) after.
Follow Program 13; continue to Program 15 (binary triangle).
In short: for each odd row length i from maxN down to 1 stepping by 2, print 1..i with line += j, then call console.log(line).
Given a positive odd integer maxN (or adjusted to odd), print an odd-length descending number triangle: row length i prints digits 1 through i, with the outer loop using i -= 2.
# maxN = 7 (conceptual shape)
# 1234567
# 12345
# 123
# 1
for (let i = maxN; i >= 1; i -= 2)
for (let j = 1; j <= i; j++)
line += j # digits 1..i
console.log(line) # next row | Item | Type | Description |
|---|---|---|
maxN | int | Maximum (odd) row width — first row prints 1..maxN. |
| Printed output | text | Only odd-length rows; each prints ascending digits 1..i. |
for i from maxN down to 1 step -2:
for j from 1 to i:
append j to line
console.log(line) | Approach | Idea | Best for |
|---|---|---|
i -= 2 outer loop | Skip even row widths | Learning and interviews |
i-- outer loop | Print every width (7, 6, 5, …, 1) | Full descending triangle comparison |
| Goal | Pattern |
|---|---|
| Walk odd row lengths | for (let i = maxN; i >= 1; i -= 2) |
Print 1..i | for (let j = 1; j <= i; j++) line += j |
| End the row | console.log(line) |
| Force odd max | if (maxN % 2 === 0) maxN -= 1 |
| All widths variant | for (let i = maxN; i >= 1; i--) (step by 1) |
| Program 13 variant | if i % 2 == 0 descending else ascending (zigzag) |
Same family of triangles — different outer-loop steps.
odd onlyPrints 7, 5, 3, 1 — skips even widths
all widthsPrints 7, 6, 5, 4, 3, 2, 1 — every row
ascendingAlways prints digits 1 through i on each row
step firstMaster step -2 before comparing with step -1
Reach for this pattern when teaching custom loop steps and skipped iterations.
Most JavaScript pattern series start here before pyramids and diamonds.
Outer/inner bound practice with an immediate visual check.
Combine loops with prompt() for a flexible row count.
Compare Program 13 (zigzag) and Program 15 (binary triangle) next.
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.
Enter an odd maximum between 1 and 20 and draw the odd-length descending triangle in the browser.
Three complete JavaScript programs — fixed maximum, configurable prompt(), and a step-by-1 comparison. Click View Output to reveal sample results, or Try it Yourself to run the code live.
Print four odd-length rows starting from maxN = 7 with a step of -2.
maxN = 7Hard-coded height — ideal for first demos and screenshots.
for (let i = 7; i >= 1; i -= 2) {
let line = "";
for (let j = 1; j <= i; j++) {
line += j;
}
console.log(line);
} When i = 7, the inner loop prints 1234567. When i = 5, it prints 12345, and so on until i = 1 prints 1. The outer loop skips even lengths because of the step -2. console.log(line) after the inner loop starts the next row.
Let the user choose the maximum row width at runtime.
Read an odd maximum with prompt() and parseInt() (check Number.isFinite in real apps); if the user enters an even number, subtract 1.
let maxN = parseInt(prompt("Enter an odd maximum (e.g., 9):"), 10);
if (!Number.isFinite(maxN) || maxN <= 0) {
console.log("Please enter a positive integer.");
} else {
if (maxN % 2 === 0) {
maxN -= 1;
}
for (let i = maxN; i >= 1; i -= 2) {
let line = "";
for (let j = 1; j <= i; j++) {
line += j;
}
console.log(line);
}
} Same nested-loop core as Example 1; maxN comes from input and is forced odd with if (maxN % 2 === 0) maxN -= 1. Non-numeric input yields NaN with bare parseInt(prompt()) — validate with Number.isFinite for safer labs.
Same ascending inner loop, but outer loop steps by 1 to include every width.
i--)Print every row width from 7 down to 1 — includes even-length rows for comparison.
const maxN = 7;
for (let i = maxN; i >= 1; i--) {
let line = "";
for (let j = 1; j <= i; j++) {
line += j;
}
console.log(line);
} Changing the outer step from -2 to -1 (using i--) includes every row width — even lengths like 123456 and 12 appear. Same inner loop; only the outer step changes the shape.
console.log is built in; use prompt() when reading input. Set maxN (fixed or from prompt, forced odd if needed).
for (let i = maxN; i >= 1; i -= 2) picks odd row lengths only; inner loop prints 1..i.
for (let j = 1; j <= i; j++) prints ascending digits with line += j on every row.
console.log(line) ends the row so the next outer iteration starts fresh.
Total digit prints: 1+2+…+n = n(n+1)/2 — O(n²) time, O(1) extra memory.
maxN = 7Trace each outer-loop value of i and note the ascending digits printed on each odd-length row.
i | Step from prev | Inner j range | Printed row |
|---|---|---|---|
7 | start | 1..7 | 1234567 |
5 | -2 | 1..5 | 12345 |
3 | -2 | 1..3 | 123 |
1 | -2 | 1..1 | 1 |
Skipped even lengths: 6, 4, 2. Total digit prints: 7+5+3+1 = 16.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: change j <= i and watch the shape change.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: change i -= 2 to i-- for a full descending triangle.
Practice print vs row newline without complex math.
Example: put console.log(line) inside the inner loop by mistake.
Swap digits for letters, stars, or spaced output once the loop works.
Example: print j + " " for spaced digits on each row.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for n = 10 still → 55.
Pair the pattern with prompt() return checks and positive-row validation.
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.
Why this pattern earns a permanent spot in beginner JavaScript courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn step -2 first; compare with step -1 to see how the loop step changes the whole shape.
Small habits that keep number-pattern code clean.
Use maxN and keep i/j for row/column — or rename to row/col.
Number.isFiniteCheck Number.isFinite(maxN) after parseInt(prompt(), 10) so bad input does not leave maxN unset.
Only call console.log(line) after the inner loop finishes the row.
if (maxN % 2 === 0) maxN -= 1 keeps the first row odd-length when reading input.
Trace maxN = 7 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.
Mistakes that commonly break odd-length descending number patterns.
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.
i-- prints every width; i -= 2 starting from an even maxN skips the intended first row.
→ For odd-only rows, use for (let i = maxN; i >= 1; i -= 2) with odd maxN.
Omitting console.log(line) glues every digit onto one endless line.
→ Always end the row after the inner loop.
Letters or empty input return NaN with bare parseInt(prompt(), 10).
→ Check Number.isFinite(maxN) and re-prompt on failure.
Switching to a 0-based outer loop without adjusting the stop value can drop the last row or print an empty first row.
→ Prefer for (let i = maxN; i >= 1; i -= 2) with for (let j = 1; j <= i; j++) for the digits.
Check these inputs before calling the solution done.
Output is just 1 on one line.
Outer loop never runs — print nothing or show a message.
maxN < 0Treat as invalid; re-prompt instead of silent empty output.
Output grows as n²/2 characters — fine for labs, noisy for huge n.
parseInt(prompt(), 10) returns NaN on bad input — validate first.
Subtract 1 or prompt again — otherwise the first row may not match the odd-only rule.
Try these variations to lock in the pattern.
i % 2j % 2 on each rowmaxN and use i -= 2n.line += j stays on the line; console.log(line) advances — mix them carefully.maxN > 0 for interactive programs; maxN = 1 should print a single 1.Quick Takeaway: outer loop steps by 2 for odd widths, inner loop prints 1..i, then break the line.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(max²) | O(1) |
| Step by 1 (Example 3) | O(max²) | O(1) |
The odd-length descending number triangle is a compact lesson in loop steps: i -= 2 skips even widths while the inner loop always prints 1..i. Master the odd-only version, then compare with i-- for a full descending triangle.
Practice the three examples above, then continue to Program 15 for the alternating binary triangle.
Use for (let i = maxN; i >= 1; i -= 2) for odd-only rows and for (let j = 1; j <= i; j++) for ascending digits — validate maxN when reading input.
-2 skips even widths before codingline += j for digits and console.log(line) after each rowmaxN ≥ 1 for interactive programsNumber.isFinite(maxN) after parseInt(prompt(), 10) before using maxNconsole.log(line) inside the inner digit loopi-- when you meant odd-only rowsmaxN = 1 edge casePrint the pattern the beginner-friendly way.
Only odd row widths
DefinitionSteps by 2 downward
CodeAlways prints 1..i
CodeEnds each row
I/OO(n²) time
AnalysisOnly 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.
Move on to the alternating binary number triangle in the JavaScript number-pattern series.
12 people found this page helpful