JavaScript Number Triangle Pattern (Left-Shifted)

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

What Is This Pattern?

A left-shifted descending triangle starts each row at a higher digit — 12345, 2345, 345, 45, 5 — so the left edge moves right.

Remember
Rule: for i = 1 to n
        print digits i..n

12345
2345
345
45
5         ← n = 5

Twin of Program 1 (12345, 1234, …, 1): same total digits, but the inner loop starts at i instead of printing 1..i.

How to Solve It

Count the row start up from 1; on each row, append digits from i through n.

MethodIdeaBest for
Nested loopsOuter i = 1..n; inner j = i..nLearning, interviews
prompt inputSame loops; read n at runtimeInteractive practice
Array joinPush digits, then join("")Shorter demos

Pseudocode

Pseudocode
for i from 1 to n:
    line = ""
    for j from i to n:
        line += j
    print line (with newline)

Cheat sheet

GoalPattern
Set sizeconst n = 5;
Outer loopfor (let i = 1; i <= n; i++)
Inner loopfor (let j = i; j <= n; j++) line += j;
End rowconsole.log(line);
Digits printedi..n (length n − i + 1)
Array joinrow.push(j); then row.join("")

Printing Numbers vs Starting a New Line

APIEffectUse for
line += jStays on the same rowEach digit
console.log(line)Ends the lineAfter the inner loop

Build the row with +=, then break once with console.log. Putting console.log inside the inner loop prints one digit per line.

Live Preview

Change n and the left-shifted triangle updates instantly.

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

Live result n = 5 · digits = 15
12345
2345
345
45
5

Worked Walkthrough — n = 3

Trace each outer value of i and the digits printed from i to n.

iDigits i..nPrinted row
11 2 3123
22 323
333

As i rises, the inner loop starts later — fewer digits, left edge shifts right.

JavaScript Programs

Three complete programs: fixed n = 5, prompt input, and an array-join builder. Use View Output for sample results, or Try It Yourself to edit and run in the playground.

Example 1 — Fixed n = 5

Hard-coded height — outer picks the start digit; inner prints through n.

JavaScript
const n = 5;

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

How It Works

1. Outer loop. i runs from 1 to n — the first digit of that row.

2. Inner loop. j runs from i to n, so line += j builds i..n.

3. Newline. console.log(line) after the inner loop prints the row and starts the next one.

Example 2 — prompt Input

Read n at runtime with prompt and parseInt.

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

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

How It Works

1. Prompt and parse. Convert the answer with parseInt(..., 10).

2. Validate first. Reject NaN or non-positive values before looping.

3. Same loops. Only the source of n changes — still print i..n on each row.

Example 3 — row.join("")

Collect digits in an array, then join once per row — same shape, no +=.

JavaScript
const n = 5;

for (let i = 1; i <= n; i++) {
  const row = [];
  for (let j = i; j <= n; j++) {
    row.push(j);
  }
  console.log(row.join(""));
}
Try It Yourself

How It Works

1. Same bounds. Outer and inner loops match Example 1.

2. Collect digits. row.push(j) stores each value instead of appending to a string.

3. Join and print. row.join("") builds the full row, then console.log ends the line.

Edge Cases & Pitfalls

Check these before calling the solution done.

log inside

Vertical digits

If console.log is inside the inner loop, each digit lands on its own line. Append with +=; log only after the inner loop.

j = 1..i

Program 1 shape

Using j = 1..i with a descending outer loop yields 12345, 1234, … — that is Program 1.

print(i)

Repeated digit

Appending i instead of j prints a repeated row digit, not a sequence.

n = 1

Single digit

Output is just 1 on one line.

n ≤ 0

Empty output

The outer loop never runs. Guard prompt input with n >= 1.

NaN input

Validate parseInt

Letters or empty prompt yield NaN — check Number.isFinite(n) && n >= 1.

Time and Space Complexity

ProgramTimeExtra space
Examples 1–2O(n²)O(n) for the current line string
Array join (Example 3)O(n²)O(n) for the row array

Total digits printed: n + (n−1) + … + 1 = n(n+1)/2 → still O(n²).

Key Takeaways

  • Rule: outer i = 1..n; print digits i..n on each row.
  • Vs Program 1: start digit rises; left edge shifts instead of shrinking from the right.
  • Write vs log: line += j builds; console.log(line) breaks.
  • Complexity: O(n²) for n rows.

One line: raise the start digit each row, and print from that digit through n.

Frequently Asked Questions

The inner loop starts at j = i, not j = 1. When i increases (1, 2, 3, …), each row begins at that value and prints until n.
Because each next row starts at a higher i, numbers below i are not appended anymore — the triangle shifts left visually.
line += j stays on the same row while building digits. console.log(line) prints the completed row and adds a newline.
Program 1 counts the outer loop down and prints 1..i (12345, 1234, …, 1). Program 2 counts up and prints i..n (12345, 2345, …, 5).
Yes. Push digits into an array and use join("") then console.log the row — same shape (see Example 3).
O(n²) for n rows because total appends are n+(n−1)+…+1 = n(n+1)/2.
Use parseInt(prompt(...), 10) and check Number.isFinite(n) && n >= 1 so bad input does not produce NaN.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.

Did you know?

Each row starts at i and prints through n, so the left edge shifts right each line — still O(n²) total appends for n rows.

Next: Reverse Descending Triangle

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

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