Unlike Program 51 (one continuous counter), each row resets m = i — total digits = n².
Approach
How to Solve It
Set m = i, append ascending, step back with m -= 2, then append descending.
Method
Idea
Best for
Two-half row
Ascend i times, m -= 2, descend i - 1 times
Classic palindrome demos
Compact
Same logic with fewer rows (e.g. 3)
Quick dry-runs
Pseudocode
Pseudocode
for i from 1 to rows:
m = i
line = ""
repeat i times: // ascending
append m; m += 1
m -= 2 // skip peak
repeat i-1 times: // descending
append m; m -= 1
print line
Cheat sheet
Goal
Pattern
Outer (rows)
for (let i = 1; i <= rows; i++)
Start of row
let m = i;
Ascending half
for (let j = 0; j < i; j++) { line += m; m++; }
Skip peak
m -= 2;
Descending half
for (let k = 0; k < i - 1; k++) { line += m; m--; }
End the row
console.log(line);
Digits on row i
2 * i - 1
Total digits
rows * rows
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += …
Stays on the same row
Each digit in both halves
console.log(line)
Ends the current row
After both halves finish
Append without a newline, then end the row once.
Try it
Live Preview
Change the row count and the palindromic pyramid updates instantly.
Whole numbers from 1 to 9 (single digits stay neat). Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 25 digits
1
232
34543
4567654
567898765
Trace
Worked Walkthrough
Trace three rows when rows = 5 — watch the ascending half, peak skip, and descending half.
Row i
Ascend
After m -= 2
Descend
Printed row
1
1
(skip)
(none)
1
2
2 3
m = 2
2
232
3
3 4 5
m = 4
4 3
34543
After ascending, m sits one past the peak — m -= 2 lands on the first descending digit.
Code
JavaScript Programs
Three complete programs: fixed rows = 5, prompt input, and a compact rows = 3 demo. Use View Output for samples, or Try It Yourself to edit and run live.
Example 1 — Fixed rows = 5
Ascend with m++, skip the peak with m -= 2, then descend with m--.
JavaScript
const rows = 5;
for (let i = 1; i <= rows; i++) {
let m = i;
let line = "";
for (let j = 0; j < i; j++) {
line += m;
m++;
}
m -= 2;
for (let k = 0; k < i - 1; k++) {
line += m;
m--;
}
console.log(line);
}
1. Start each row at i.m = i so row 3 begins at 3, row 5 at 5.
2. Ascend to the peak. Loop i times with m++ — for i = 3 that appends 345.
3. Skip the peak, then descend.m -= 2 avoids a double middle digit; the second loop appends 43 → 34543.
Example 2 — User Input Rows
Read rows with prompt and validate before drawing.
JavaScript
const rows = parseInt(prompt("Enter number of rows:"), 10);
if (!Number.isFinite(rows) || rows < 1) {
console.log("Please enter a positive integer.");
} else {
for (let i = 1; i <= rows; i++) {
let m = i;
let line = "";
for (let j = 0; j < i; j++) {
line += m;
m++;
}
m -= 2;
for (let k = 0; k < i - 1; k++) {
line += m;
m--;
}
console.log(line);
}
}
1. Prompt and validate. Use parseInt and require rows >= 1.
2. Same two-half core. Only the source of rows changes — ascend, peak skip, descend match Example 1.
3. Entering 4. Sixteen digits total (4²), last row 4567654.
Example 3 — Compact rows = 3
Same ascend / peak-skip / descend logic with fewer rows for a quick visual check.
JavaScript
const rows = 3;
for (let i = 1; i <= rows; i++) {
let m = i;
let line = "";
for (let j = 0; j < i; j++) {
line += m;
m++;
}
m -= 2;
for (let k = 0; k < i - 1; k++) {
line += m;
m--;
}
console.log(line);
}
1. Shrink the size. Only rows changes — still ascend, skip peak, descend.
2. Easier to trace. Nine digits total — useful while learning m -= 2.
3. Same width rule. Row i still has exactly 2i - 1 digits.
Edge Cases & Pitfalls
Check these before calling the solution done.
Peak skip
Always use m -= 2 before descending
Skipping this step repeats the middle digit (e.g. 345543 instead of 34543).
Descend count
Run the descending loop i - 1 times
Ascending already printed the peak — the mirror half is one shorter.
rows = 1
Single-row case
Output is just 1 — the descending loop is skipped.
Bad input
Validate parseInt
Check Number.isFinite(rows) and require rows >= 1 before the loops.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / compact (Examples 1, 3)
O(n²)
O(n) per row string
User input (Example 2)
O(n²)
O(n) per row string
Row i prints 2i - 1 digits; summing odd numbers gives n² total work.
Remember
Key Takeaways
Rule: ascend i..(2i-1), then descend back to i.
Peak skip:m -= 2 prevents printing the middle digit twice.
Break the row: call console.log(line) only after both halves finish.
Complexity:O(n²) — odd digits per row sum to n².
One line: for each row, ascend from i, skip the peak with m -= 2, then descend and log.
Frequently Asked Questions
Row 3 starts at 3, appends up to 5 (345), then after m -= 2 appends back down to 3 (43) — producing 34543.
After the increasing loop, m is one past the peak. Subtracting 2 lands on the value just before the peak so the decreasing loop does not repeat the middle digit.
Each row counts up from i to 2i-1, then back down to i — the same sequence forwards and backwards.
line += appends on the same row. console.log(line) ends the row after both halves finish.
Program 51 uses one continuous counter across rows. Program 52 resets m = i each row and builds a palindromic line.
O(n²) for n rows because row i logs 2i-1 digits and 1+3+…+(2n-1) = n².
Yes — use line += m + " " in both loops and trim before console.log if needed.
One row prints 1 — the decreasing loop never runs when i = 1.
🤔
Did you know?
Each row is palindromic: append i..(2i-1) ascending, then back down after m -= 2 skips the peak. Row 3 logs 34543 — total digits = 1+3+5+…+(2n-1) = n².