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.
Approach
How to Solve It
Count the row start up from 1; on each row, append digits from i through n.
Method
Idea
Best for
Nested loops
Outer i = 1..n; inner j = i..n
Learning, interviews
prompt input
Same loops; read n at runtime
Interactive practice
Array join
Push 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
Goal
Pattern
Set size
const n = 5;
Outer loop
for (let i = 1; i <= n; i++)
Inner loop
for (let j = i; j <= n; j++) line += j;
End row
console.log(line);
Digits printed
i..n (length n − i + 1)
Array join
row.push(j); then row.join("")
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += j
Stays on the same row
Each digit
console.log(line)
Ends the line
After 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.
Try it
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 resultn = 5 · digits = 15
12345
2345
345
45
5
Trace
Worked Walkthrough — n = 3
Trace each outer value of i and the digits printed from i to n.
i
Digits i..n
Printed row
1
1 2 3
123
2
2 3
23
3
3
3
As i rises, the inner loop starts later — fewer digits, left edge shifts right.
Code
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);
}
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);
}
}