Number Mountain Pattern (Increase then Decrease)

What You’ll Learn
How to print a number “mountain” where each row starts at the row number, climbs up, then comes back down:
1, 232, 34543, 4567654, 567898765.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1
232
34543
4567654
567898765Complete JavaScript Program
We use a variable m that starts at i, increases for i steps, then decreases for i-1 steps to build a symmetric row.
const rows = 5;
for (let i = 1; i <= rows; i++) {
let m = i;
let line = "";
for (let j = 1; j <= i; j++) {
line += m;
m++;
}
m -= 2;
for (let k = 1; k < i; k++) {
line += m;
m--;
}
console.log(line);
}🧠 How It Works
Loop through rows
i = 1..rows decides how wide each mountain row is.
Start from i
m = i means row 3 starts at 3, row 4 starts at 4, and so on.
Print the increasing part
The first inner loop prints i values: m, m+1, ....
Step back and print decreasing part
We do m -= 2 to avoid repeating the peak, then print i-1 values while decrementing.
Symmetric mountain row
Row i always forms a symmetric peak with length 2i-1.
Variation — Browser (document.write) Version
Print the same mountain pattern in an HTML page using document.write:
<!DOCTYPE html>
<html>
<body>
<script>
var rows = 5;
for (var i = 1; i <= rows; i++) {
var m = i;
for (var j = 1; j <= i; j++) document.write(m++);
m = m - 2;
for (var k = 1; k < i; k++) document.write(m--);
document.write("<br>");
}
</script>
</body>
</html>💡 Tips for Enhancement
Try These
- Increase
rowsto generate a taller mountain - Add spaces between digits for readability (use
line += m + " ") - Start from a custom base number instead of
i - Mirror the whole triangle vertically to create a diamond
Avoid
- Forgetting
m -= 2(you’ll repeat the peak value) - Printing
ivalues on the second loop (it should bei-1) - Omitting the newline after each row
Key Takeaways
Row i has 2i - 1 digits.
The first loop prints the increasing part.
The second loop prints the decreasing part after stepping back.
Two-phase row construction is a common pattern-printing technique.
❓ Frequently Asked Questions
4567654.trimEnd() when printing the final line.m to a fixed value before the first loop (instead of m = i).m -= 2 step and start decreasing from m - 1 so the peak prints twice.Explore More JavaScript Number Patterns!
Try adding spaces and centering this mountain to turn it into a pyramid-style pattern.
The total number of digits printed up to row n is \(1 + 3 + 5 + \dots + (2n-1) = n^2\).
12 people found this page helpful
