Increasing Number Triangle Using i + j - 1 in JavaScript

Beginner
⏱️ 9 min read
📚 Updated: Sep 2026
🎯 3 Code Examples
🚀 Live Preview
Formula + Loops

What You’ll Learn

The increasing number triangle using i + j - 1 prints 1, 2 3, 3 4 5, … — a natural follow-up after Program 32’s triangle starting from 11. This tutorial covers the i + j - 1 formula, nested loops, a live preview, worked JavaScript examples, edge cases, and complexity.

Shape Rule

Left-shifted triangle

Row i prints i numbers computed as i + j - 1.

Outer Loop

i = 1..rows

for (let i = 1; i <= rows; i++) — one growing row per iteration.

Inner Loop (j)

1..i

for (let j = 1; j <= i; j++) — appends i values per row.

Formula

i + j - 1

Each value is i + j - 1 — row i starts at i when j = 1.

Live Preview

3–9 rows

Pick a row count and draw the increasing triangle in the browser.

O(n²)

Complexity

Prints per row = i — total work scales as .

Introduction

A left-shifted increasing number triangle prints values from the formula i + j - 1 on each row. With rows = 5, you get 1, 2 3, 3 4 5, and so on.

In JavaScript you use nested loops: outer i = 1..rows, inner j = 1..i, appending (i + j - 1) with a trailing space.

Why it matters?

It combines nested loops with a compact arithmetic formula — a step after Program 32’s 9 + i + j offset.

Key Highlights

i + j - 1

Formula for each value.

Inner j <= i

Growing row width.

Starts at 1

When i=1, j=1 → 1.

Series Foundation

Follow Program 32; continue to Program 34 (i + j from 0) next.

In short: outer loop i = 1..rows, inner j = 1..i, print i + j - 1 with a space, then console.log(line).

📝 Problem & Approach

Given rows = 5, print a left-shifted increasing triangle: for each row i, print j = 1..i values of i + j - 1 separated by spaces.

JavaScript
// rows = 5 (conceptual shape)
const rows = 5;
for (let i = 1; i <= rows; i++) {
  let line = "";
  for (let j = 1; j <= i; j++) {
    line += (i + j - 1) + " ";
  }
  console.log(line);
}

Inputs & Outputs

ItemTypeDescription
rowsintTriangle height — number of lines to print.
iintOuter loop — current row; also part of the formula.
jintInner loop — column index; runs 1..i per row.

Minimal workflow

Pseudocode
for i from 1 to rows:
    for j from 1 to i:
        line += (i + j - 1) + " "
    console.log(line)

Approach comparison

ApproachIdeaBest for
Fixed formula1, 2 3, …Learning and interviews
User-input rowsparseInt(prompt(...), 10)Configurable triangle size
Compact tracerows = 3 on paper firstDebugging loop bounds

⚡ Quick Reference

GoalPattern
Outer loopfor (let i = 1; i <= rows; i++)
Inner loopfor (let j = 1; j <= i; j++)
Print valueline += (i + j - 1) + " "
End the rowconsole.log(line)
User inputparseInt(prompt(...), 10)

📋 Fixed vs User Input vs Compact Demo

Same increasing triangle — different ways to control the row count.

Outer loop
i = 1..rows

One growing row per iteration

Formula
i + j - 1

Starts at 1

Inner loop
j = 1..i

i values per row

Learning tip
j = 1 → i

Row starts at row number

Context

When This Pattern Shows Up

Reach for this pattern when teaching formula-based output, growing inner loops, and arithmetic in nested loops.

  1. After Program 32

    Natural follow-up after the triangle starting from 11 — uses i + j - 1 to start at 1.

  2. Nested-loop warm-up

    Outer/inner bound practice with an immediate visual check.

  3. Console I/O practice

    Combine loops with prompt() for a flexible row count.

  4. Gateway to variants

    Compare Program 32 (9 + i + j) and Program 34 (i + j from 0) next.

  5. Not a UI layout tool

    This is a console teaching pattern — not how you build modern app screens.

Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.

🔮 Live Preview

Choose a row count between 3 and 9 and draw the increasing triangle in the browser.

Try 4, 5, or 7. Max up to 9 in this preview.

Live result
Press "Draw pattern".

Examples Gallery

Three complete JavaScript programs — fixed rows, user input, and a smaller trace demo. Click View Output to reveal sample console results, or Try it Yourself to run the code live.

📚 Getting Started

Print five rows of the increasing triangle with the i + j - 1 formula.

Example 1 — Fixed rows = 5

Hard-coded row count — ideal for first demos and screenshots.

JavaScript
const rows = 5;

for (let i = 1; i <= rows; i++) {
  let line = "";
  for (let j = 1; j <= i; j++) {
    line += (i + j - 1) + " ";
  }
  console.log(line);
}
Try it Yourself

How It Works

When i = 1, the inner loop prints 1+1-1 = 1. When i = 4, it prints 4, 5, 6, 7 — output 4 5 6 7.

📈 User Input

Read the row count with prompt() and parseInt() instead of hard-coding 5.

Example 2 — User input rows

Read rows with prompt() and parseInt() instead of hard-coding 5.

JavaScript
const rowsInput = prompt("Enter rows:");
const rows = parseInt(rowsInput, 10);

if (!Number.isFinite(rows) || rows < 1) {
  console.log("Please enter a positive integer.");
} else {
  for (let i = 1; i <= rows; i++) {
    let line = "";
    for (let j = 1; j <= i; j++) {
      line += (i + j - 1) + " ";
    }
    console.log(line);
  }
}
Try it Yourself

How It Works

Same formula core as Example 1; only rows comes from user input instead of being hard-coded as 5. Non-numeric input yields NaN with bare parseInt(prompt(), 10) — validate with Number.isFinite for safer labs.

⚡ Smaller Demo

Run with rows = 3 to trace every row on paper before scaling up.

Example 3 — Compact rows = 3

Same nested-loop formula with a smaller row count for quick tracing.

JavaScript
const rows = 3;

for (let i = 1; i <= rows; i++) {
  let line = "";
  for (let j = 1; j <= i; j++) {
    line += (i + j - 1) + " ";
  }
  console.log(line);
}
Try it Yourself

How It Works

Only rows changes from 5 to 3 — the nested-loop formula stays identical. Trace i = 1, 2, 3 on paper to see how each row adds one more value.

🧠 How the Algorithm Prints Rows

1

Set up

No imports needed for fixed rows; use prompt() when reading. Set const rows = 5 and loop variables i, j.

Setup
2

Outer loop walks rows

for (let i = 1; i <= rows; i++) — one growing row per iteration.

Row
3

Inner loop (j)

for (let j = 1; j <= i; j++) — appends i values per row.

Grow
4

Print formula

line += (i + j - 1) + " " — each value from the arithmetic formula.

Formula
5

New line

console.log(line) ends the row after the inner loop finishes.

Break
=

Increasing triangle complete

Prints per row = iO(n²) time, O(1) extra memory.

🔎 Worked Walkthrough — rows = 5

Trace each outer-loop value of i, inner-loop range, values printed, and full row output.

iInner range (j)Values (i+j-1)Row output
1111
21, 22, 32 3
31, 2, 33, 4, 53 4 5
41..44, 5, 6, 74 5 6 7
51..55, 6, 7, 8, 95 6 7 8 9

Prints per row = i — total prints = n(n+1)/2 for n rows.

Use Cases

Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.

1. Teaching Nested Loops

Clearest visual proof that outer and inner bounds interact.

Example: change inner bound to j <= rows and watch every row print the same width.

2. Pattern Series Base

Foundation for formula-based triangles and left-shifted sequences starting at 1.

Example: continue to Program 34 for the i + j variant starting from 0.

3. Console Formatting Drills

Practice line += vs row newline without complex math.

Example: put console.log(line) inside the inner loop by mistake.

4. Padding character

Add spaces between digits once the two-loop structure works.

Example: use line += (i + j - 1) + " " between digits for wider spacing.

5. Complexity Intuition

Triangular totals make O(n²) concrete for beginners.

Example: count printed numbers for rows = 5 — total is 1+2+3+4+5 = 15.

6. Input Validation Labs

Pair the pattern with Number.isFinite and positive-row checks.

Example: reject rows <= 0 and re-prompt.

Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.

Advantages

Why this pattern earns a permanent spot in beginner JavaScript courses.

  1. 1. Instant Visual Feedback

    Wrong bounds show up immediately as a broken staircase.

  2. 2. Minimal Concepts

    Only loops and console output — no arrays or math libraries.

  3. 3. Easy to Extend

    Invert, center, hollow, or change the fill character with small edits.

  4. 4. O(1) Extra Memory

    Streaming output needs no storage beyond loop counters.

Pro Tip: trace i and j on paper for rows = 3 before coding — watch how row i starts at i when j = 1.

Usage Tips

Small habits that keep number-pattern code clean.

  1. 1. Growing Inner Loop

    Inner bound must be j <= i — row i prints exactly i numbers.

  2. 2. Validate with Number.isFinite

    Use Number.isFinite so bad input does not produce NaN when converting rows.

  3. 3. Keep console.log(line) Outside the Inner Loop

    Only call console.log(line) after the inner loop finishes the row.

  4. 4. Trace i + j - 1 on Paper

    Write the formula for each (i, j) pair before coding the loops.

  5. 5. Dry-Run rows = 3

    Trace i = 1..3 on paper before coding the full rows = 5 demo.

Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put console.log(line) inside the inner loop.

Common Pitfalls

Mistakes that commonly break increasing number triangles.

  1. 1. Newline Inside the Inner Loop

    Each digit lands on its own line — you get a column, not a triangle.

    → Use line += (i + j - 1) + " "; console.log(line) only after the inner loop.

  2. 2. Wrong Formula

    Using i + j or 9 + i + j shifts every value — rows no longer start at the row number.

    → Keep i + j - 1 so row i begins at i.

  3. 3. Wrong Inner Bound

    j <= rows prints a rectangle — every row has the same width.

    → Keep for (let j = 1; j <= i; j++) so row i prints i values.

  4. 4. Missing Trailing Space

    Printing numbers without a space makes multi-digit values run together on wider rows.

    → Append a space after each number: line += (i + j - 1) + " ".

  5. 5. Bare parseInt(prompt())

    Letters or empty input yield NaN with bare parseInt(prompt(), 10).

    → Validate with Number.isFinite and re-prompt on failure.

Edge Cases

Check these inputs before calling the solution done.

rows = 1

Single number row

Output is just 1 — one value, one row.

rows = 0

Empty pattern

Outer loop never runs — print nothing or show a message.

Negative

rows < 0

Treat as invalid; re-prompt instead of silent empty output.

rows = 2

Smallest triangle

Two rows: 1 and 2 3.

Bad input

Non-numeric input

Bare parseInt(prompt(), 10) yields NaN on bad input — validate with Number.isFinite first.

Large rows

Large row count

Total prints = n(n+1)/2 — grows quadratically with row count.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Triangle from 0

  • Continue with Program 34
  • Formula i + j with i starting at 0

2. Triangle from 11

  • Review Program 32
  • Formula 9 + i + j instead of i + j - 1

3. Row starts at i

  • Prove on paper: when j = 1, i + j - 1 = i
  • Each row adds one more consecutive number

4. Safe input loop

  • Validate rows >= 1 after reading input
  • Then draw the triangle

Notes

  • Formula rule. Each value is i + j - 1. Inner loop runs j = 1..i — row i prints i numbers.
  • line += (i + j - 1) + " " stays on the line; console.log(line) advances — mix them carefully.
  • Validate rows > 0 for interactive programs; rows = 1 prints a single 1.
  • When j = 1, the value is always i — compare with Program 34 where the formula is i + j.

Quick Takeaway: outer loop i = 1..rows, inner j = 1..i, print i + j - 1, then console.log(line).

⏱️ Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–3)O(n²)O(1)
Smaller demo (Example 3)O(n²)O(1)
Wrap Up

🎉 Conclusion

The increasing number triangle using i + j - 1 is a compact lesson in formula-based nested loops: compute each value with i + j - 1, grow the inner bound to i, and end each row with console.log(line). Master the fixed-rows version, then try user input and a smaller trace demo.

Practice the three examples above, then continue to Program 34 for the i + j variant starting from 0.

Inner bound must be j <= i — validate rows when reading from the console.

💡 Best Practices

✅ Do

  • Use for (let i = 1; i <= rows; i++) in the outer loop
  • Inner: for (let j = 1; j <= i; j++) appends i values
  • Formula: line += (i + j - 1) + " "
  • Validate parseInt(prompt(), 10) with Number.isFinite
  • State O(n²) time when asked about complexity

❌ Don’t

  • Call console.log(line) inside the inner loop
  • Use j <= rows in the inner loop (prints a rectangle)
  • Forget the trailing space after each number
  • Ignore bad console input in user-facing demos
  • Skip the rows = 1 edge case

Key Takeaways

Knowledge Unlocked

Five things to remember about this increasing triangle

Print the pattern the beginner-friendly way.

5
Core concepts
02

Inner bound

j = 1..i

Code
1 03

Row start

j=1 → i

Code
04

Row break

console.log(line) after the inner loop

Shape
O 05

Complexity

O(n²) time

Analysis

❓ Frequently Asked Questions

Because when j = 1, the expression i + j - 1 becomes i. Row 4 therefore starts with 4.
j increases by 1, so i + j - 1 increases by 1 as well — producing consecutive numbers on each row.
Program 32 uses 9 + i + j (starts at 11). Program 33 uses i + j - 1 (starts at 1).
Program 33 uses i + j - 1 with i starting at 1. Program 34 uses i + j with i starting at 0.
line += (i + j - 1) + " " keeps values separated on the same row. console.log(line) ends the row.
Replace 5 with rows in the outer loop bound — see Example 2.
O(n²) for n rows because total appends are 1 + 2 + ... + n = n(n+1)/2.
Use parseInt with Number.isFinite or validate the raw string before converting so bad input does not produce NaN.
Only one row prints — a single 1.

Did you Know? 🔊

Each printed value is computed as i + j - 1. Row i = 1 prints 1; row i = 4 prints 4, 5, 6, 7 — a left-shifted increasing triangle starting at 1.

Continue to Program 34

Move on to the increasing number triangle starting from 0 (i + j) in the JavaScript number-pattern series.

Program 34 tutorial →

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