Shifted Number Triangle (Starts at 11) in JavaScript

What You’ll Learn
How to print a triangle of numbers where each value is computed using a small formula: 9 + i + j.
You’ll practice nested loops and learn how changing constants affects the starting value of the entire pattern.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
11
12 13
13 14 15
14 15 16 17
15 16 17 18 19Complete JavaScript Program
Row i prints i values. Each value is offset + i + j where offset = 9.
const rows = 5;
const offset = 9;
for (let i = 1; i <= rows; i++) {
let line = "";
for (let j = 1; j <= i; j++) {
line += (offset + i + j) + (j === i ? "" : " ");
}
console.log(line);
}🧠 How It Works
Set rows and offset
rows controls height, and offset = 9 shifts the entire pattern upward.
Outer loop controls rows
i goes from 1..5. Each row prints exactly i numbers.
Inner loop prints values
j runs from 1..i and prints offset + i + j.
Spacing keeps rows readable
We add a space between numbers but not after the last number in each row.
Row values depend on i + j
That’s why 13 appears at the end of row 2 and the start of row 3.
Variation — Browser (document.write) Version
Print the triangle in an HTML page using document.write:
<!DOCTYPE html>
<html>
<body>
<script>
var rows = 5;
for (var i = 1; i <= rows; i++) {
for (var j = 1; j <= i; j++)
document.write(9 + i + j + " ");
document.write("<br>");
}
</script>
</body>
</html>💡 Tips for Enhancement
Try These
- Change
offsetto shift the start (e.g., 0-based, 10-based) - Make
rowsuser input and validate it - Use
padStartto align columns for larger values - Replace the formula with
i * jto print multiplication-style triangles
Avoid
- Leaving trailing spaces in console output if you care about exact formatting
- Using string concatenation without separators (numbers can run together)
- Hard-coding constants when you want a reusable pattern function
Key Takeaways
Each row prints a growing count: 1, 2, 3, 4, 5…
Each value comes from a simple formula (offset + i + j).
Changing a constant offset shifts the entire pattern.
Spacing is important when printing multi-digit numbers.
❓ Frequently Asked Questions
9 + 5 + 1 = 15.offset = -1 so the first value becomes -1 + 1 + 1 = 1.i + j, so values overlap between rows (for example, 13 appears in both row 2 and row 3).Explore More JavaScript Number Patterns!
Formula-based patterns are great practice for turning math into loops.
Many “triangle” patterns are just nested loops where the inner loop limit depends on the outer loop. Changing the printed expression (like i + j) creates entirely new sequences.
12 people found this page helpful
