Shape Rule
* on X + center
* when on a diagonal or center column; 0 fills every other cell.

Program 45 prints an X-style grid: * on both diagonals and the center column, 0 everywhere else on a 4 × 9 rectangle — a natural step after Program 44’s centered number diamond. This tutorial covers nested loops with multi-condition checks, a live preview, worked JavaScript examples, edge cases, and complexity.
* on X + center
* when on a diagonal or center column; 0 fills every other cell.
i = 1..rows
for (let i = 1; i <= rows; i++) walks each row of the grid.
j = 1..cols
for (let j = 1; j <= cols; j++) walks each column within the current row.
Diagonals + mid
i === j || j === mid || i === cols + 1 - j appends *; else append 0.
4×9 default
Adjust rows and odd column width, then draw the star-and-zero X in the browser.
Complexity
Each cell visited once — rows × cols prints; extra memory stays O(1).
A star-and-zero X pattern prints * on both diagonals and the center column, filling every other cell with 0. With rows = 4 and cols = 9, the output forms a compact cross on a rectangular grid.
In JavaScript the outer loop runs i = 1..rows, the inner loop runs j = 1..cols, and a three-part condition uses line += "*" or line += "0" per cell.
It teaches diagonal math and multi-condition checks on a 2D grid — a key step after Program 44’s centered diamond.
i === j left-to-right.
i === cols + 1 - j.
Program 44 prints ascending digits in a diamond; Program 45 prints * and 0 on a fixed grid.
Follow Program 44; continue to Program 46 next.
In short: nested loops over i, j, three-way check appends *, else 0, then console.log(line).
Given a 4 × 9 grid, print * on both diagonals and the center column; fill remaining cells with 0.
// rows = 4, cols = 9
//*000*000*
//0*00*00*0
//00*0*0*00
//000***000 | Item | Type | Description |
|---|---|---|
rows | number | Number of rows (e.g. 4). |
cols | number | Number of columns (e.g. 9 — odd width gives a clear center). |
mid | number | Center column: Math.floor((cols + 1) / 2) (e.g. 5 when cols is 9). |
i | number | Outer loop — current row index (1 to rows). |
j | number | Inner loop — current column index (1 to cols). |
mid = Math.floor((cols + 1) / 2)
for i from 1 to rows:
for j from 1 to cols:
if (i === j || j === mid || i === cols + 1 - j):
append "*"
else:
append "0"
console.log(line) | Approach | Idea | Best for |
|---|---|---|
| Nested loops + condition | *000*000* fixed 4×9 | Learning and interviews |
| Parameterized rows/cols | mid = Math.floor((cols + 1) / 2) | Flexible rectangular grids |
| Diagonals only | Drop j === mid check | Pure X without center line |
| Goal | Pattern |
|---|---|
| Walk rows | for (let i = 1; i <= rows; i++) |
| Walk columns | for (let j = 1; j <= cols; j++) |
| Center column | mid = Math.floor((cols + 1) / 2) |
| Star check | if (i === j || j === mid || i === cols + 1 - j) |
| Append star | line += "*" |
| Append fill | line += "0" |
| Program 44 contrast | Centered number diamond with ascending digits — not a star/zero grid |
Same star-and-zero X — different ways to control dimensions and which lines print stars.
i = 1..rowsRows of the grid
j = 1..colsColumns per row
mid = (cols+1)/2Vertical line column
i==j or j==midThree-way star check
Reach for this pattern when teaching 2D grids, diagonal math, and multi-condition cell checks.
Natural follow-up after Program 44 — same nested loops but adds diagonal and center conditions.
Practice i === j and i + j === cols + 1 on paper before coding.
Unlike square patterns, rows and cols can differ — center column needs odd width.
Compare Program 44 (number diamond) and Program 46 (next in series) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, diagonal conditions, and O(rows×cols) thinking.
Set rows (3–6) and odd column width (7–11), then draw the star-and-zero X in the browser.
Three complete JavaScript programs — fixed grid, parameterized dimensions, and diagonals-only contrast. Click View Output to reveal sample console results, or Try it Yourself to run the code live.
Print a 4×9 star-and-zero X with nested loops and a three-part condition.
rows = 4, cols = 9Hard-coded grid dimensions — ideal for first demos and screenshots.
const rows = 4;
const cols = 9;
const mid = 5; // middle column (1-based)
for (let i = 1; i <= rows; i++) {
let line = "";
for (let j = 1; j <= cols; j++) {
if (i === j || j === mid || i === cols + 1 - j) {
line += "*";
} else {
line += "0";
}
}
console.log(line);
} When i = 1 and j = 1, i === j is true — appends *. When i = 2 and j = 5, j === 5 hits the center column — appends *. All other cells append 0.
Use rows, cols, and mid instead of hard-coded 4, 9, and 5.
Compute mid with Math.floor((cols + 1) / 2) and use (cols + 1) - j for the anti-diagonal.
const rows = 4;
const cols = 9;
const mid = Math.floor((cols + 1) / 2);
for (let i = 1; i <= rows; i++) {
let line = "";
for (let j = 1; j <= cols; j++) {
if (i === j || j === mid || i === cols + 1 - j) {
line += "*";
} else {
line += "0";
}
}
console.log(line);
} Same inner-loop core as Example 1; mid replaces the literal 5, and (cols + 1) - j replaces 10 - j. Change rows or cols to resize the pattern.
Drop the center-column check for a pure X without the vertical line.
Remove j === mid from the condition — only the two diagonals append stars.
const rows = 4;
const cols = 9;
for (let i = 1; i <= rows; i++) {
let line = "";
for (let j = 1; j <= cols; j++) {
if (i === j || i === cols + 1 - j) {
line += "*";
} else {
line += "0";
}
}
console.log(line);
} Without the center column, row 4 no longer prints 000***000 — it becomes 000*0*000. Compare with Example 1 to see how one condition changes the shape.
No imports needed. Set rows = 4, cols = 9, and loop variables i, j for the grid.
for (let i = 1; i <= rows; i++) and for (let j = 1; j <= cols; j++) visit every cell in the grid.
if (i === j || j === mid || i === cols + 1 - j) — true on a diagonal or center column.
Matching cells use line += "*"; all others use line += "0".
console.log(line) ends each row after the inner loop finishes.
Every cell visited once — O(rows×cols) time, O(1) extra memory.
i = 3, cols = 9Trace each column j on row 3 — which cells match a diagonal or center condition.
j | Condition | Prints |
|---|---|---|
1 | No | 0 |
2 | No | 0 |
3 | i === j | * |
4 | No | 0 |
5 | j === mid | * |
6 | No | 0 |
7 | i === cols + 1 - j | * |
8 | No | 0 |
9 | No | 0 |
Row 3 output: 00*0*0*00 — stars at columns 3, 5, and 7. Total cells = rows × cols = 36 for a 4×9 grid.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: swap 0 for . or space — see FAQ.
Foundation for X patterns, cross grids, and diagonal-only variants.
Example: continue to Program 46 for the next pattern in the series.
Practice line += vs console.log(line) without complex math.
Example: put console.log(line) inside the inner loop by mistake.
Learn why i === j and i + j === cols + 1 mark the two diagonals.
Example: trace row i = 3 in the walkthrough table.
Rectangular totals make O(rows×cols) concrete for beginners.
Example: count star cells for 4×9 — total grid cells = 36.
Pair the pattern with Number.isFinite and positive-row validation.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the 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.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Drop center column, change fill char, or resize rows/cols with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace row i = 3 and each j on paper — watch how one cell can match multiple conditions at intersections.
Small habits that keep number-pattern code clean.
Never hard-code 5 or 10 — use mid and (cols + 1) - j everywhere.
Number.isFiniteUse Number.isFinite so bad prompt() input does not crash when converting dimensions.
Only call console.log(line) after the inner loop finishes the row.
Use odd cols so mid = Math.floor((cols + 1) / 2) lands on a single center column.
Trace columns j = 1..9 on paper before coding the full 4×9 demo.
Pro Tip: if the output is a vertical list of single characters per line, you almost certainly put console.log(line) inside the inner loop.
Mistakes that commonly break star-and-zero X patterns.
Each cell lands on its own line — you get a column, not a square.
→ Use line += "*" or line += "0" per cell; console.log(line) only after the inner loop.
Using 10 - j breaks when cols changes from 9 to 11 or 7.
→ Always use (cols + 1) - j for the anti-diagonal.
Only checking diagonals gives a pure X — missing the vertical line in the full pattern.
→ Add j === mid where mid = Math.floor((cols + 1) / 2).
Even cols has no single middle column — mid may not align as expected.
→ Prefer odd column counts (7, 9, 11) for a clear center line.
parseInt(prompt())Letters or empty input yield NaN with bare parseInt(prompt()).
→ Check Number.isFinite and re-prompt on failure.
Check these inputs before calling the solution done.
One row of stars and zeros — diagonals collapse to corner cells only.
Outer loop never runs — print nothing or show a message.
Even column width has no single middle — center line may look off.
Smaller width — mid = 4, anti-diagonal uses 8 - j.
Bare parseInt(prompt()) yields NaN on bad input — use Number.isFinite first.
Each cell visited once — total work grows as rows × cols.
Try these variations to lock in the pattern.
j === mid from the condition. or space instead of 0* when i === j || j === mid || i === cols + 1 - j; else append 0.line +=, then console.log(line) once per row.cols for a clear center column; compute mid = Math.floor((cols + 1) / 2).rows × cols grid has rows × cols cells — each visited exactly once.Quick Takeaway: nested loops over i, j, three-way check appends *, else 0, then console.log(line).
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(rows × cols) | O(1) |
| Smaller demo (Example 3) | O(rows × cols) | O(1) |
The star-and-zero X pattern is a compact nested-loop lesson: visit every cell in a rectangular grid and use a three-part condition to print * or 0. Master the fixed 4×9 version, then try parameterized dimensions and the diagonals-only variant.
Practice the three examples above, then continue to Program 46 for the next pattern in the series.
Diagonals use i === j and i === cols + 1 - j — add j === mid for the center column and prefer odd column width.
for (let i = 1; i <= rows; i++) and for (let j = 1; j <= cols; j++)if (i === j || j === mid || i === cols + 1 - j)"*" on match, "0" elsewhere with line +=mid = Math.floor((cols + 1) / 2) for center columncols for a clear vertical lineconsole.log(line) inside the inner cell loop10 - j when cols can changej === mid if you want the center columni = 3 before codingPrint the pattern the beginner-friendly way.
* on X + center
DefinitionRows i = 1..rows
CodeColumns j = 1..cols
Codei==j or j==mid
LogicO(rows×cols)
AnalysisAppend * when i === j, j === mid, or i === cols + 1 - j; otherwise append 0. A rows × cols grid visits every cell once — total appends = rows × cols.
Move on to the next pattern in the JavaScript number-pattern series.
12 people found this page helpful