JavaScript Palindrome Number Triangle Pattern (Outer Peak)

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

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.

How to Solve It

Walk i from 1 to rows; append the left descent, then the right ascent.

MethodIdeaBest for
Two inner loopsLeft i..2; right 1..iLearning, interviews
prompt rowsSame loops; read rows at runtimeInteractive practice
Compact rows = 3Same structure; easier to trace by handPaper 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

GoalPattern
Set rowsconst rows = 5;
Outer loopfor (let i = 1; i <= rows; i++)
Left halffor (let j = i; j >= 2; j--) line += j;
Right halffor (let j = 1; j <= i; j++) line += j;
End rowconsole.log(line);
Digits per row2 * i - 1
vs Program 27Peak at i vs valley at 1

Printing Numbers vs Starting a New Line

APIEffectUse for
line += jStays on the same rowEach digit
console.log(line)Ends the lineAfter both halves

Build the row 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
212
32123
4321234
543212345

Worked Walkthrough — rows = 3

Trace each outer i, the left descent, the right ascent, and the printed row.

iLeft i..2Right 1..iPrinted row
1none11
221 2212
33 21 2 332123

The left loop stops at 2 so the ascending half can print the center 1.

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

How It Works

1. Outer loop. i runs from 1 to 5 — the outer edge digit of that palindrome.

2. Left half. Append j from i down to 2 (empty when i = 1).

3. Right half. Append j from 1 to i; then console.log(line).

Example 2 — prompt Rows

Read rows at runtime with prompt and parseInt.

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 = i; j >= 2; j--) {
      line += j;
    }
    for (let j = 1; j <= i; j++) {
      line += j;
    }
    console.log(line);
  }
}
Try It Yourself

How It Works

1. Prompt and validate. Parse the answer; reject non-positive or non-numeric input.

2. Same shape. Both halves still use i — only the outer bound changes.

3. Center stays 1. Every row still valleys at 1 between the two loops.

Example 3 — Compact rows = 3

Same two-loop structure with a smaller row count — easy to trace on paper.

JavaScript
const rows = 3;

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

How It Works

1. Same structure. Outer and both inner loops match Example 1.

2. Only rows changes. Total digits = 1+3+5 = 9 = 3².

3. Trace first. Walk i = 1, 2, 3 on paper before scaling to 5 or more.

Edge Cases & Pitfalls

Check these before calling the solution done.

stop at 1

Double center

If the left loop runs down to 1, you print two ones in a row. Stop at 2.

swap halves

Broken mirror

Ascending first then descending yields Program 27’s shape, not this valley-at-1 form.

log inside

Vertical digits

If console.log is inside either inner loop, each digit lands on its own line.

rows = 1

Single digit

Output is just 1 — the left loop never runs.

multi-digit

Values above 9

When i > 9, cells become two characters and the visual palindrome breaks. Keep demos at rows ≤ 9.

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

Digits per row = 2i - 1; total = 1 + 3 + … + (2n-1) = n² → O(n²).

Key Takeaways

  • Rule: print i..2, then 1..i, on each row.
  • Stop left at 2: the ascending half owns the center 1.
  • Write vs log: line += j builds; console.log(line) breaks.
  • Complexity: total digits = n² → O(n²).

One line: mirror around 1 by descending to 2, then climbing back up to i.

Frequently Asked Questions

Each row reads the same forward and backward. For example, 4321234 is symmetric around the center digit 1.
First, a loop appends i down to 2 (left half). Then another loop appends 1 up to i (right half). Together they create a mirrored sequence.
The first loop appends 4 3 2, and the second loop appends 1 2 3 4, which together form 4321234.
Row i appends 2i - 1 digits — one more than the previous row.
Program 27 prints 1..i then i-1..1 (peak at i). Program 37 prints i..2 then 1..i (valley at 1).
Replace 5 with rows in the outer loop bound — see Example 2.
O(n²) for n rows because total digits appended are 1 + 3 + 5 + ... + (2n-1) = n².
Use parseInt with Number.isFinite so bad input does not produce NaN.
Only one row prints — a single digit 1.

Did you know?

Each row is a palindrome: append i down to 2, then 1 up to i. Row i appends 2i - 1 digits — total digits across all rows = n².

Next: Sequential Decreasing-Width Triangle

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

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