JavaScript Palindrome Number Triangle Pattern

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

What Is This Pattern?

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.

How to Solve It

For each row i, append 1..i, then append i−1..1, then end the line.

MethodIdeaBest for
Two inner loopsAscend, then mirror from i − 1Learning, interviews
prompt inputSame loops; read row count at runtimeInteractive practice
Spaced digitsline += j + " " in both halvesReadable 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

GoalPattern
Set rowsconst rows = 5;
Outer loopfor (let i = 1; i <= rows; i++)
Ascending halffor (let j = 1; j <= i; j++) line += j;
Mirror halffor (let k = i - 1; k >= 1; k--) line += k;
End rowconsole.log(line);
Spacedline += j + " "; / line += k + " ";

Printing Numbers vs Starting a New Line

APIEffectUse for
line += j / line += kStays on the same rowEach digit
console.log(line)Ends the lineAfter 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.

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 result rows = 5 · digits = 25
1
121
12321
1234321
123454321

Worked Walkthrough — rows = 3

Trace each i, the ascending half, the mirror half, and the full row.

iAscend 1..iMirror i−1..1Printed row
11none1
2121121
31232112321

Starting the mirror at i − 1 keeps the peak digit from appearing twice in the middle.

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);
}
Try It Yourself

How It Works

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);
  }
}
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 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);
}
Try It Yourself

How It Works

1. Same structure. Outer loop and both half-loops match Example 1.

2. Only append changes. Use line += j + " " and line += k + " ".

3. Still a palindrome. Values and length stay the same — only spacing differs.

Edge Cases & Pitfalls

Check these before calling the solution done.

k = i

Doubled peak

Starting the mirror at k = i repeats the middle digit (123321). Use k = i − 1.

no mirror

Missing second loop

Skipping the descending loop prints only 1, 12, 123 — not palindromes.

log inside

Vertical digits

If console.log is inside either inner loop, each digit lands on its own line. Log only after both loops.

rows = 1

Single digit

Output is just 1 — the mirror loop never runs.

rows ≤ 0

Empty output

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

NaN input

Validate parseInt

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

Time and Space Complexity

ProgramTimeExtra space
Examples 1–3O(n²)O(n) for the current line string

Row i prints 2i − 1 digits; total = 1 + 3 + … + (2n − 1) = n² → O(n²).

Key Takeaways

  • Rule: print 1..i, then mirror i − 1..1.
  • Start at i − 1: avoids doubling the peak digit in the middle.
  • Write vs log: line += … builds; console.log(line) breaks.
  • Complexity: O(n²) for n rows.

One line: climb to i, then walk back down from i − 1 so the row is a palindrome.

Frequently Asked Questions

Starting from i − 1 avoids repeating the middle number. For i = 3, appending 123 then 21 makes 12321.
Use line += j + " " and line += k + " " in both loops instead of line += j (see Example 3).
The ascending loop appends 1..i and the descending loop appends i − 1..1 — together they read the same forwards and backwards.
line += j appends digits on the same line. console.log(line) ends the row after both inner loops finish.
One loop handles the ascending half (j) and the other handles the mirror half (k) — clearer than a single complex loop.
Replace 5 with rows in the outer loop bound — see Example 2.
O(n²) for n rows because row i prints 2i − 1 digits and the total is 1+3+5+…+(2n−1) = n².
Use parseInt(prompt(...), 10) and check Number.isFinite(rows) && rows >= 1 so bad input does not produce NaN.
Only one row prints — a single 1 with no mirror half.

Did you know?

This palindrome triangle appends 1..i and then i-1..1 on each row. The second loop mirrors the first, producing outputs like 12321 and 123454321.

Next: 0-Centered Descending Mirror

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

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