JavaScript Alternating Number Triangle Pattern (Zigzag)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
An alternating zigzag triangle shrinks each row while flipping print direction — odd lengths ascend 1..i, even lengths descend i..1.
Remember
Rule: for i = n down to 1
if i is odd → print 1..i
if i is even → print i..1
12345
4321
123
21
1 ← n = 5
Combines a shrinking outer loop with an if/else on i % 2 — sequential digits, not repeated ones like Program 12.
Approach
How to Solve It
Shrink row length from n; use parity to choose ascending or descending digits.
Method
Idea
Best for
if/else branches
Odd → 1..i; even → i..1
Learning, interviews
prompt input
Same logic; read n at runtime
Interactive practice
Direction vars
One inner loop with start/end/step
Cleaner refactor
Pseudocode
Pseudocode
for i from n down to 1:
line = ""
if i is even:
for j from i down to 1:
line += j
else:
for j from 1 to i:
line += j
print line (with newline)
Cheat sheet
Goal
Pattern
Set size
const n = 5;
Outer loop
for (let i = n; i >= 1; i--)
Odd row
for (let j = 1; j <= i; j++) line += j;
Even row
for (let j = i; j >= 1; j--) line += j;
Parity test
i % 2 === 0
End row
console.log(line);
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += j
Stays on the same row
Each sequential 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 zigzag 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
4321
123
21
1
Trace
Worked Walkthrough — n = 3
Trace each outer value of i, its parity, and the printed direction.
i
Parity
Direction
Printed row
3
odd
1..3
123
2
even
2..1
21
1
odd
1..1
1
As i drops, rows shorten; i % 2 flips ascending vs descending.
Code
JavaScript Programs
Three complete programs: fixed n = 5, prompt input, and a direction-variable refactor. Use View Output for sample results, or Try It Yourself to edit and run in the playground.
const n = 5;
for (let i = n; i >= 1; i--) {
let line = "";
if (i % 2 === 0) {
for (let j = i; j >= 1; j--) {
line += j;
}
} else {
for (let j = 1; j <= i; j++) {
line += j;
}
}
console.log(line);
}
1. Outer loop.i runs from n down to 1 — the length of that row.
2. Parity branch. Even i prints i..1; odd i prints 1..i via line += j.
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 = n; i >= 1; i--) {
let line = "";
if (i % 2 === 0) {
for (let j = i; j >= 1; j--) {
line += j;
}
} else {
for (let j = 1; j <= i; j++) {
line += j;
}
}
console.log(line);
}
}
1. Compute direction. Even i sets descending start/end/step; odd i sets ascending.
2. One inner loop. The same for walks either way using those three values.
3. Same shape. Output matches Examples 1–2 — useful once the if/else rule is clear.
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.
print(i)
Repeated digit
Appending i instead of j prints a repeated digit, not a sequence — that is Program 11/12 territory.
Wrong parity
Mirrored zigzag
Swapping the if/else branches flips which rows ascend — still valid, but not the sample output.
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.
Analysis
Time and Space Complexity
Program
Time
Extra space
Examples 1–2
O(n²)
O(n) for the current line string
Direction vars (Example 3)
O(n²)
O(n) for the current line string
Total digits printed: n + (n−1) + … + 1 = n(n+1)/2 → still O(n²).
Remember
Key Takeaways
Rule: outer i = n..1; odd → 1..i, even → i..1.
Vs Program 12: sequential digits + parity flip, not repeated digits.
Write vs log:line += j builds; console.log(line) breaks.
Complexity:O(n²) for n rows.
One line: shrink the row from n, and let i % 2 choose ascending or descending digits.
Frequently Asked Questions
The program checks whether i is even or odd. For odd i it prints 1..i ascending; for even i it prints i..1 descending, creating a zigzag direction.
The second row from the top has length i = 4, which is even. The even branch runs for (let j = i; j >= 1; j--) and prints 4, 3, 2, 1.
line += j stays on the same row while building digits. console.log(line) prints the completed row and adds a newline.
Program 12 repeats the row digit (11111, 2222, 333). Program 13 prints sequential digits 1..i or i..1 and alternates direction using i % 2.
O(n²) where n is the number of rows. Total digit appends equal n+(n−1)+…+1 = n(n+1)/2.
Yes. Swap the if and else branches so odd rows descend and even rows ascend — the zigzag still works, just mirrored.
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?
Odd row length i prints 1..i; even row length prints i..1. The outer loop shrinks from n to 1, and i % 2 flips direction — still O(n²) total digit prints.