Odd-Length Descending Number Rows in JavaScript

What You’ll Learn
How to print an odd-length descending row pattern in JavaScript where each row starts from 1 and ends at a shrinking odd limit.
This pattern is great for learning custom loop steps like i -= 2.
⭐ Pattern Output
For starting value i = 7, the pattern looks like this:
1234567
12345
123
1Complete JavaScript Program
Outer loop reduces row length by 2 each time. Inner loop prints numbers from 1 up to current i.
for (let i = 7; i >= 1; i -= 2) {
let line = "";
for (let j = 1; j <= i; j++) {
line += j;
}
console.log(line);
}🧠 How It Works
Start at an odd width
i = 7 makes the first row 1234567.
Outer loop (step by -2)
for (let i = 7; i >= 1; i -= 2) keeps row lengths odd: 7, 5, 3, 1.
Inner loop prints 1..i
for (let j = 1; j <= i; j++) appends each number in ascending order to build one row.
Print and continue
console.log(line); outputs the current row, then next odd width is processed.
Odd-length descending rows
Custom loop steps like i -= 2 are useful for creating selective row sizes and skipping values.
Variation — Browser (document.write) Version
Print the same pattern directly in an HTML page using document.write:
<!DOCTYPE html>
<html>
<body>
<script>
for (let i = 7; i >= 1; i -= 2) {
for (let j = 1; j <= i; j++) {
document.write(j);
}
document.write("<br>");
}
</script>
</body>
</html>💡 Tips for Enhancement
Try These
- Take starting size from user input (for example 9 or 11)
- Switch to
i -= 1to print every row length - Use even starts to generate 8, 6, 4, 2 style output
- Add spaces between digits for readability
- Render pattern in a
<pre>block with DOM APIs
Avoid
- Using wrong outer step when you need odd-only widths
- Changing inner-loop bounds that break 1..i sequence
- Skipping input validation for non-positive start values
- Using
document.writefor production UIs
Key Takeaways
The outer loop controls row lengths and skips by 2 each step.
The inner loop always prints ascending numbers from 1 to current i.
Odd-length rows create a distinct shrinking sequence: 7,5,3,1.
Loop step size is a powerful tool for selective pattern generation.
❓ Frequently Asked Questions
i -= 2, it only visits odd lengths.for (let i = 9; i >= 1; i -= 2).document.write.Explore More JavaScript Number Patterns!
Practice step-based loop patterns to gain stronger control over row selection and output shape.
Using a custom loop step like i -= 2 is an easy way to filter rows by parity without adding extra if checks.
12 people found this page helpful
