JavaScript Increasing Number Triangle Pattern (Right-Aligned)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A right-aligned number triangle prints 1 to i on row i, with leading spaces so shorter rows sit against the right edge.

Remember
Rule: spaces = rows - i, then append 1..i

    1
   1 2
  1 2 3
 1 2 3 4
1 2 3 4 5     ← rows = 5

Unlike Program 42 (hollow square), this pattern grows one extra number each row — total numbers for n rows is n(n+1)/2.

How to Solve It

One outer row loop, leading spaces for indent, then an ascending number loop. End each row with console.log.

MethodIdeaBest for
Right-aligned" ".repeat(rows - i), then 1..iClassic triangle demos
Left-alignedSkip leading spaces; append 1..i onlySimpler first dry-runs

Pseudocode

Pseudocode
for i from 1 to rows:
    line = spaces of length (rows - i)
    for k from 1 to i:
        append k and a space
    print line

Cheat sheet

GoalPattern
Outer (rows)for (let i = 1; i <= rows; i++)
Leading spaceslet line = " ".repeat(rows - i);
Append 1..ifor (let k = 1; k <= i; k++) line += k + " ";
End the rowconsole.log(line);
Total numbersrows * (rows + 1) / 2

Printing Numbers vs Starting a New Line

APIEffectUse for
line += …Stays on the same rowSpaces and each number
console.log(line)Ends the current rowAfter the row is fully built

Append without a newline, then end the row once.

Live Preview

Change the row count and the right-aligned triangle updates instantly.

Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result rows = 5 · 15 numbers
    1 
   1 2 
  1 2 3 
 1 2 3 4 
1 2 3 4 5 

Worked Walkthrough

Trace spaces and the ascending sequence when rows = 4.

Row iSpacesNumbersPrinted row
1311
221 21 2
311 2 31 2 3
401 2 3 41 2 3 4

Space count is rows - i. Total numbers for n rows = n(n+1)/2.

JavaScript Programs

Three complete programs: fixed rows = 5, prompt input, and a left-aligned contrast. Use View Output for samples, or Try It Yourself to edit and run live.

Example 1 — Fixed rows = 5

Hard-coded height — leading spaces, then ascending 1..i on each row.

JavaScript
const rows = 5;

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

How It Works

1. Walk each row. Outer loop i runs from 1 to rows.

2. Push right with spaces. " ".repeat(rows - i) adds more indent on shorter rows.

3. Append 1..i. Inner loop appends each number plus a space, then console.log(line) ends the row.

Example 2 — User Input Rows

Read the row count with prompt and validate before drawing.

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

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

How It Works

1. Prompt and validate. Use parseInt and reject non-positive values.

2. Same triangle core. Only the source of rows changes — spaces and 1..i match Example 1.

3. Entering 3. Six numbers total (3×4/2), ending at 1 2 3.

Example 3 — Left-Aligned Contrast

Same ascending numbers, without leading-space indentation.

JavaScript
const rows = 5;

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

How It Works

1. Drop the left pad. Start each line as "" instead of repeating spaces.

2. Keep the number loop. Still append k + " " for k = 1..i.

3. Same sequence, different shape. Useful for comparing with the right-aligned Example 1.

Edge Cases & Pitfalls

Check these before calling the solution done.

Space formula

Use rows - i, not a hard-coded count

Hard-coding spaces for 5 rows breaks when the height changes.

log early

Call console.log only after the row is built

Logging inside the number loop prints one digit per line.

rows = 1

Single-row case

Output is just 1 — repeat(0) adds no spaces.

Bad input

Validate parseInt

Check Number.isFinite(rows) and require rows >= 1 before the loops.

Time and Space Complexity

ProgramTimeExtra space
Right-aligned (Examples 1–2)O(n²)O(n) per row string
Left-aligned (Example 3)O(n²)O(n) per row string

Numbers appended are 1 + 2 + … + n = n(n+1)/2 — quadratic in n. Each row builds a temporary string proportional to the row width.

Key Takeaways

  • Rule: on row i, append numbers 1 to i.
  • Align right: leading spaces of length rows - i.
  • Break the row: call console.log(line) only after spaces and numbers finish.
  • Complexity: O(n²) time — total numbers = n(n+1)/2.

One line: pad left with rows - i spaces, append 1..i, then log the row.

Frequently Asked Questions

A right-aligned ascending triangle: row 1 prints 1, row 2 prints 1 2, row 3 prints 1 2 3, and so on.
Before appending numbers, add leading spaces with " ".repeat(rows - i). Smaller rows get more spaces, pushing digits to the right.
Yes. Change line += k + " " to line += String(k) if you want digits to touch.
Skip the leading-space step. Just append numbers from 1 to i on each row — see Example 3.
line += appends on the same row. console.log(line) ends the row after spaces and numbers finish.
Program 42 prints a hollow square with border conditions. Program 43 prints an ascending number triangle with indentation spaces.
O(n²) for n rows because total appends are 1+2+…+n = n(n+1)/2.
Only one row prints — a single 1 with no leading spaces.

Did you know?

Each row appends numbers 1 to i. Leading spaces use " ".repeat(rows - i) before the number loop; total numbers for n rows = n(n+1)/2.

Next: Diamond Number Pyramid

Continue with the next pattern in the JavaScript number-pattern series.

Program 44 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