Left-Decreasing Right-Fixed Number Pattern in JavaScript

What You’ll Learn
How to print a two-part row pattern where the left side changes by row and the right side stays filled with a fixed number.
This pattern is useful for practicing multi-loop row composition in JavaScript.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
5 5 5 5 5
4 5 5 5 5
3 4 5 5 5
2 3 4 5 5
1 2 3 4 5Complete JavaScript Program
First inner loop prints an increasing segment from i to 5. Second loop pads remaining columns with 5.
const rows = 5;
for (let i = rows; i >= 1; i--) {
let line = "";
for (let j = i; j <= rows; j++) {
line += j + " ";
}
for (let j = 1; j < i; j++) {
line += rows + " ";
}
console.log(line.trimEnd());
}🧠 How It Works
Set total rows
const rows = 5; defines width and height.
Outer loop (descending i)
for (let i = rows; i >= 1; i--) determines row start value.
Print left segment (i..rows)
The first inner loop appends increasing values from current i up to rows.
Pad right segment with fixed 5
The second loop appends repeated rows values to fill remaining width.
Split-row number composition
Combining two inner loops lets you build rows from multiple logical parts with independent rules.
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 = 5; i >= 1; i--) {
for (let j = i; j <= 5; j++) {
document.write(j + " ");
}
for (let j = 1; j < i; j++) {
document.write("5 ");
}
document.write("<br>");
}
</script>
</body>
</html>💡 Tips for Enhancement
Try These
- Replace fixed tail value
5with any constant - Use dynamic
rowsinput for scalable patterns - Align columns using padding for table-like output
- Convert the pattern to descending left segment variation
- Render in DOM with
<pre>for clean spacing
Avoid
- Mixing loop bounds and breaking row width consistency
- Forgetting the second loop that pads with fixed value
- Skipping trim/spacing cleanup in console output
- Using
document.writein production pages
Key Takeaways
One loop can create the changing left side (i..5).
A second loop can fill the fixed right side with repeated values.
Descending outer loop naturally shifts row starts from 5 down to 1.
Split-loop rows are useful for matrix-like pattern design.
❓ Frequently Asked Questions
i = 5, first loop prints only 5, and second loop adds four more 5s.i = 1, first loop prints full sequence to 5, and second loop runs zero times.document.write to display the same pattern in HTML.Explore More JavaScript Number Patterns!
Learn to combine multiple loops per row to create structured and layered pattern outputs.
Many interview-style pattern problems are solved by splitting each row into segments and handling each segment with its own loop.
12 people found this page helpful
