Shifted Number Triangle (Starts at 11) in JavaScript

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Formula-based printing

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:

Output
11
12 13
13 14 15
14 15 16 17
15 16 17 18 19
1

Complete JavaScript Program

Row i prints i values. Each value is offset + i + j where offset = 9.

JavaScript
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

1

Set rows and offset

rows controls height, and offset = 9 shifts the entire pattern upward.

Setup
2

Outer loop controls rows

i goes from 1..5. Each row prints exactly i numbers.

Rows
3

Inner loop prints values

j runs from 1..i and prints offset + i + j.

Formula
4

Spacing keeps rows readable

We add a space between numbers but not after the last number in each row.

Formatting
=

Row values depend on i + j

That’s why 13 appears at the end of row 2 and the start of row 3.

2

Variation — Browser (document.write) Version

Print the triangle in an HTML page using document.write:

HTML
<!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 offset to shift the start (e.g., 0-based, 10-based)
  • Make rows user input and validate it
  • Use padStart to align columns for larger values
  • Replace the formula with i * j to 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

1

Each row prints a growing count: 1, 2, 3, 4, 5…

2

Each value comes from a simple formula (offset + i + j).

3

Changing a constant offset shifts the entire pattern.

4

Spacing is important when printing multi-digit numbers.

❓ Frequently Asked Questions

On row 5, the first value is 9 + 5 + 1 = 15.
Yes. Set offset = -1 so the first value becomes -1 + 1 + 1 = 1.
Not exactly. It’s based on i + j, so values overlap between rows (for example, 13 appears in both row 2 and row 3).
Remove the `" "` in the output, but note that multi-digit numbers will run together and become hard to read.

Explore More JavaScript Number Patterns!

Formula-based patterns are great practice for turning math into loops.

All Number Patterns →
Did you know?

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful