Shape Rule
Palindromic row
Row i prints 1..i ascending, then i-1..1 descending — always reads the same forward and backward.

Program 56 prints a centered palindromic number pyramid: each row shows 1..i..1 with leading spaces for centering — a natural step after Program 55’s column-wise triangle. This tutorial covers spacing, ascending and descending loops, a live preview, worked JavaScript examples, edge cases, and complexity.
Palindromic row
Row i prints 1..i ascending, then i-1..1 descending — always reads the same forward and backward.
i = 1..rows
for (let i = 1; i <= rows; i++) picks the current row and its palindromic width.
s = 1..(rows-i)
line += " " repeated rows - i times — centers the pyramid.
k = 1..i
for (let k = 1; k <= i; k++) line += k + " " — counts up to the peak.
k = i-1..1
for (let k = i - 1; k >= 1; k--) line += k + " " — mirrors without repeating the peak.
Complexity
Row i prints about 2i-1 digits plus spaces — total work grows as O(n²).
A palindromic number pyramid prints a centered triangle where row i shows digits from 1 up to i and back down to 1. With rows = 5, you get 1, 1 2 1, 1 2 3 2 1, and so on — each row wider and centered with leading spaces.
In JavaScript use an outer loop for rows, append (rows - i) space pairs to line, then ascending 1..i, then descending i-1..1, before console.log(line.trimEnd()).
It bridges Program 55’s column-wise fill to centered symmetry — combining spacing with ascending and descending loops on each row.
(rows - i) pairs of spaces before digits.
k = 1..i prints up to the peak.
Program 55 uses column-wise 2D array fill; Program 56 logs palindromic rows with spacing.
Follow Program 55; continue to Program 57 next.
In short: outer i = 1..rows, spaces rows-i, ascending 1..i, descending i-1..1, then console.log(line.trimEnd()).
Given row count rows = 5, print a centered palindromic number pyramid — row i shows 1..i..1 with leading spaces.
// rows = 5
// 1
// 1 2 1
// 1 2 3 2 1
//1 2 3 4 3 2 1
//1 2 3 4 5 4 3 2 1 | Item | Type | Description |
|---|---|---|
rows | int | Pyramid height — bottom row has rows as peak digit. |
i (outer) | int | Current row index — runs 1 to rows. |
s (spaces) | int | Appends (rows - i) pairs of spaces for centering. |
k (ascending) | int | Appends 1..i with trailing space after each digit. |
k (descending) | int | Appends i-1..1 — skips repeating the peak digit. |
| Row width | int | Row i has 2i-1 digits plus leading spaces. |
for i from 1 to rows:
append (rows - i) pairs of spaces to line
for k from 1 to i:
append k + " " to line
for k from i - 1 down to 1:
append k + " " to line
console.log(line.trimEnd()) | Approach | Idea | Best for |
|---|---|---|
| Three inner loops | Spaces, ascending 1..i, descending i-1..1 | Learning and interviews |
| Skip peak in descent | k = i-1..1 avoids duplicate peak digit | Clean palindromic rows |
| User-input rows | prompt() + parseInt() | Flexible pyramid size |
| Compact trace | rows = 3 on paper first | Quick dry-runs before full demo |
| Full diamond | Mirror bottom half from rows-1..1 | Extension after mastering pyramid |
| Goal | Pattern |
|---|---|
| Outer loop | for (let i = 1; i <= rows; i++) |
| Leading spaces | for (let s = 0; s < rows - i; s++) line += " " |
| Ascending half | for (let k = 1; k <= i; k++) line += k + " " |
| Descending half | for (let k = i - 1; k >= 1; k--) line += k + " " |
| End row | console.log(line.trimEnd()) |
| Program 55 contrast | Program 55 uses column-wise 2D fill; Program 56 uses centered palindromic rows |
Same centered pyramid — three ways to set row count and trace the logic.
rows = 5Hard-coded height for demos
parseInt(prompt())Read row count from console
rows = 3Quick dry-run on paper
rows - iSpace pairs before digits
1..i..1Digits per row i
Reach for this pattern when teaching centered output, palindromic sequences, and combining spacing with multiple inner loops.
Natural follow-up after Program 55’s column-wise triangle — introduces centering and palindromic rows.
Each row reads symmetrically — good bridge to string palindrome problems.
Spaces plus ascending and descending halves — concrete nested-loop practice.
Compare this centered pyramid with the next pattern in the series.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in centering, palindromic rows, and O(n²) thinking.
Choose row count between 3 and 9 and draw the centered palindromic number pyramid in the browser.
Three complete JavaScript programs — fixed rows, user input, and a compact trace demo. Click View Output to reveal sample console results, or Try it Yourself to run in the browser.
Print a centered palindromic pyramid with five rows — spaces, ascending, and descending loops per row.
rows = 5Hard-coded row count — append leading spaces, then ascending 1..i, then descending i-1..1, then log the row.
const rows = 5;
for (let i = 1; i <= rows; i++) {
let line = "";
for (let s = 0; s < rows - i; s++) {
line += " ";
}
for (let k = 1; k <= i; k++) {
line += k + " ";
}
for (let k = i - 1; k >= 1; k--) {
line += k + " ";
}
console.log(line.trimEnd());
} When i = 3, print 2 space pairs, then 1 2 3, then 2 1 — output 1 2 3 2 1. When i = 1, only the ascending loop runs and the descending loop is skipped.
Read row count with prompt() and Number.isFinite validation.
Read rows with prompt() and validate the result.
const rowsInput = prompt("Enter the number of rows:");
const rows = parseInt(rowsInput, 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 s = 0; s < rows - i; s++) {
line += " ";
}
for (let k = 1; k <= i; k++) {
line += k + " ";
}
for (let k = i - 1; k >= 1; k--) {
line += k + " ";
}
console.log(line.trimEnd());
}
} Same spacing and palindromic loop core as Example 1; only the source of rows changes from a literal to user input.
Smaller row count for quick tracing on paper or in interviews.
rows = 3Use rows = 3 to trace spacing, ascending, and descending loops before scaling to 5 rows.
const rows = 3;
for (let i = 1; i <= rows; i++) {
let line = "";
for (let s = 0; s < rows - i; s++) {
line += " ";
}
for (let k = 1; k <= i; k++) {
line += k + " ";
}
for (let k = i - 1; k >= 1; k--) {
line += k + " ";
}
console.log(line.trimEnd());
} With only three rows you can trace every space pair and digit loop on paper before running the full rows = 5 demo.
const rows = 5; controls pyramid height and spacing width.
for (let s = 0; s < rows - i; s++) line += " " — centers row i.
for (let k = 1; k <= i; k++) line += k + " " — counts up to the peak.
for (let k = i - 1; k >= 1; k--) line += k + " " then console.log(line.trimEnd()).
Row i prints 2i-1 digits — O(n²) time, O(1) extra memory.
rows = 5Trace each row’s space count, ascending half, descending half, and full line output.
i | Space pairs | Ascending | Descending | Row output |
|---|---|---|---|---|
1 | 4 | 1 | (skip) | 1 |
2 | 3 | 1 2 | 1 | 1 2 1 |
3 | 2 | 1 2 3 | 2 1 | 1 2 3 2 1 |
4 | 1 | 1 2 3 4 | 3 2 1 | 1 2 3 4 3 2 1 |
5 | 0 | 1 2 3 4 5 | 4 3 2 1 | 1 2 3 4 5 4 3 2 1 |
Row i always prints exactly 2i-1 digits — a palindromic line built from spacing plus two inner loops.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Spacing plus ascending and descending loops — three inner loops per row.
Example: trace each row in the walkthrough table — spaces, ascending, descending.
Each row reads symmetrically — compare with Program 52's left-aligned palindrome.
Example: row 5 has no leading spaces and shows 1 2 3 4 5 4 3 2 1.
Practice building one line string per row instead of logging inside inner loops.
Example: call console.log() inside the inner loop by mistake.
Space pairs on row i = rows - i — fewer spaces as the pyramid widens.
Example: Peak row 10 has 9 space pairs on row 1 — 19 digits on the bottom row.
Growing inner bound makes O(n²) concrete — count prints for n rows.
Example: Peak row 5 bottom line has 9 digits — see the walkthrough table.
Pair the pattern with Number.isFinite and positive-row validation after parseInt(prompt()).
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner JavaScript courses.
Each row is instantly recognizable as a palindrome — spacing, ascending, and descending halves make the shape obvious.
Centering with spaces teaches real console alignment — not abstract loop drill.
Mirror the bottom half from rows-1..1 to build a full diamond, or use fixed-width formatting for larger peaks.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace rows = 3 on paper — row 2 shows 1 2 1 with 1 space pair.
Small habits that keep number-pattern code clean.
Append (rows - i) pairs of two spaces to line before digits on row i.
Avoid crashing when the user types letters instead of a number.
Only call console.log(line.trimEnd()) after all three inner loops finish the row.
Loop for (let k = i - 1; k >= 1; k--) so the peak digit is not logged twice.
Trace five rows on paper before coding the full 10-row demo.
Pro Tip: if the output is a vertical list of single numbers, you almost certainly called console.log() inside the inner loop.
Mistakes that commonly break palindromic number pyramid patterns.
Each digit lands on its own line — you get a column, not a pyramid.
→ Build line with +=; call console.log(line.trimEnd()) only after all three inner loops.
Starting descending at k = i prints the peak digit twice — row looks like 1 2 2 1.
→ Use for (let k = i - 1; k >= 1; k--) — skip the peak in the descending half.
Without leading spaces the pyramid is left-aligned, not centered.
→ Print (rows - i) pairs of two spaces before the digits on row i.
All numbers print on one long line without row breaks.
→ Call console.log(line.trimEnd()) after all inner loops complete.
Letters or empty input yield NaN or leave rows invalid.
→ Validate with Number.isFinite(rows) and check rows >= 1 before drawing.
Check these inputs before calling the solution done.
Output is just 1 — the descending loop does not run.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Bottom row has 9 digits — good for dry-runs before scaling up.
Bare parseInt(prompt()) returns NaN — validate with Number.isFinite.
Row 9 scans 17 character positions — total work grows as O(n²).
Try these variations to lock in the pattern.
String(k).padStart(2) when appending digits for rows beyond 9s = 1..(rows-i). Ascending: k = 1..i. Descending: k = i-1..1.line string per row with += — call console.log(line.trimEnd()) only after all three inner loops finish.rows > 0 for interactive programs; rows = 1 prints a single centered 1.i prints 2i-1 digits — total work grows as O(n²) for n rows.Quick Takeaway: spaces rows-i, ascending 1..i, descending i-1..1, then console.log(line.trimEnd()).
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Digits on row i | 2i - 1 | No storage beyond loop counters |
The palindromic number pyramid is a natural follow-up to Program 55: centered rows built with spacing, ascending, and descending loops. Master the fixed-rows version, then try user input and the compact 3-row trace.
Practice the three examples above, then continue to Program 57 for the next pattern in the series.
Row i prints 2i-1 palindromic digits — centered with (rows-i) space pairs.
for (let s = 0; s < rows - i; s++) line += " "for (let k = 1; k <= i; k++) line += k + " "for (let k = i - 1; k >= 1; k--) line += k + " "console.log(line.trimEnd()) after all three inner loopsNumber.isFinite after parseInt(prompt()) for user inputk = i — duplicates the peak digitconsole.log() inside any inner looprows = 3 dry-run before coding rows = 5Print the centered pyramid the beginner-friendly way.
1..i..1 per row
Definitionrows - i spaces
Codek = 1..i
Codek = i-1..1
LogicO(n²) time
AnalysisEach row prints 1..i..1 with leading spaces for centering. Row i has 2i-1 digits — total prints grow as O(n²) for n rows.
Move on to the next pattern in the JavaScript number-pattern series.
12 people found this page helpful