JavaScript Palindrome Number Triangle Pattern (Outer Peak)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
A palindrome number triangle prints a mirrored row each time — descend i..2, then ascend 1..i, so every line reads the same forwards and backwards.
Remember
Rule: for i = 1..rows
print i..2, then 1..i
1
212
32123
4321234
543212345 ← rows = 5
Unlike Program 27’s peak-at-i palindrome (12321), this valley-at-1 form is a natural step after Program 36.
Approach
How to Solve It
Walk i from 1 to rows; append the left descent, then the right ascent.
Method
Idea
Best for
Two inner loops
Left i..2; right 1..i
Learning, interviews
prompt rows
Same loops; read rows at runtime
Interactive practice
Compact rows = 3
Same structure; easier to trace by hand
Paper walkthrough
Pseudocode
Pseudocode
for i from 1 to rows:
line = ""
for j from i down to 2:
line += j
for j from 1 to i:
line += j
print line (with newline)
Cheat sheet
Goal
Pattern
Set rows
const rows = 5;
Outer loop
for (let i = 1; i <= rows; i++)
Left half
for (let j = i; j >= 2; j--) line += j;
Right half
for (let j = 1; j <= i; j++) line += j;
End row
console.log(line);
Digits per row
2 * i - 1
vs Program 27
Peak at i vs valley at 1
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 both halves
Build the row with +=, then break once with console.log. Putting console.log inside an inner loop prints one digit per line.
Try it
Live Preview
Change the row count and the palindrome triangle updates instantly.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · digits = 25
1
212
32123
4321234
543212345
Trace
Worked Walkthrough — rows = 3
Trace each outer i, the left descent, the right ascent, and the printed row.
i
Left i..2
Right 1..i
Printed row
1
none
1
1
2
2
1 2
212
3
3 2
1 2 3
32123
The left loop stops at 2 so the ascending half can print the center 1.
Code
JavaScript Programs
Three complete programs: fixed rows = 5, prompt rows, and compact rows = 3. Use View Output for sample results, or Try It Yourself to edit and run in the playground.
Example 1 — Fixed rows = 5
Hard-coded height — descend i..2, then ascend 1..i.
JavaScript
const rows = 5;
for (let i = 1; i <= rows; i++) {
let line = "";
for (let j = i; j >= 2; j--) {
line += j;
}
for (let j = 1; j <= i; j++) {
line += j;
}
console.log(line);
}