A palindrome number triangle prints ascending digits 1..i, then mirrors i−1..1 — each row reads the same forwards and backwards.
Remember
Rule: for i = 1..rows
print 1..i, then i−1..1
1
121
12321
1234321
123454321 ← rows = 5
Two inner loops keep the peak digit from repeating — a clear follow-up after the diagonal star in Program 26.
Approach
How to Solve It
For each row i, append 1..i, then append i−1..1, then end the line.
Method
Idea
Best for
Two inner loops
Ascend, then mirror from i − 1
Learning, interviews
prompt input
Same loops; read row count at runtime
Interactive practice
Spaced digits
line += j + " " in both halves
Readable output
Pseudocode
Pseudocode
for i from 1 to rows:
line = ""
for j from 1 to i:
line += j
for k from i - 1 down to 1:
line += k
print line (with newline)
Cheat sheet
Goal
Pattern
Set rows
const rows = 5;
Outer loop
for (let i = 1; i <= rows; i++)
Ascending half
for (let j = 1; j <= i; j++) line += j;
Mirror half
for (let k = i - 1; k >= 1; k--) line += k;
End row
console.log(line);
Spaced
line += j + " "; / line += k + " ";
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += j / line += k
Stays on the same row
Each digit
console.log(line)
Ends the line
After both inner loops
Build the full palindrome 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
121
12321
1234321
123454321
Trace
Worked Walkthrough — rows = 3
Trace each i, the ascending half, the mirror half, and the full row.
i
Ascend 1..i
Mirror i−1..1
Printed row
1
1
none
1
2
12
1
121
3
123
21
12321
Starting the mirror at i − 1 keeps the peak digit from appearing twice in the middle.
Code
JavaScript Programs
Three complete programs: fixed rows = 5, prompt input, and spaced digits. 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 — ascend 1..i, then mirror from i − 1.
JavaScript
const rows = 5;
for (let i = 1; i <= rows; i++) {
let line = "";
for (let j = 1; j <= i; j++) {
line += j;
}
for (let k = i - 1; k >= 1; k--) {
line += k;
}
console.log(line);
}
1. Outer rows.i runs from 1 to rows — the peak digit of that palindrome.
2. Ascend. Append j from 1 to i with no spaces.
3. Mirror. Append k from i − 1 down to 1; then console.log(line).
Example 2 — prompt Input
Read the row count at runtime; both inner loops stay the same.
JavaScript
const rows = parseInt(prompt("Enter 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 = "";
for (let j = 1; j <= i; j++) {
line += j;
}
for (let k = i - 1; k >= 1; k--) {
line += k;
}
console.log(line);
}
}
1. Prompt and parse. Convert the answer with parseInt(..., 10).
2. Validate first. Reject NaN or non-positive values before looping.
3. Same two loops. Ascend and mirror logic matches Example 1 for the chosen height.
Example 3 — Spaced Digits
Keep rows = 5 but append a space after each digit in both halves.
JavaScript
const rows = 5;
for (let i = 1; i <= rows; i++) {
let line = "";
for (let j = 1; j <= i; j++) {
line += j + " ";
}
for (let k = i - 1; k >= 1; k--) {
line += k + " ";
}
console.log(line);
}