An X pattern of * and 0 fills a grid: stars on both diagonals and the center column; zeros everywhere else.
Remember
Rule: * if i===j or j===mid or i===cols+1-j; else 0
*000*000*
0*00*00*0
00*0*0*00
000***000 ← rows = 4, cols = 9
Unlike Program 44 (number diamond), this pattern visits every cell and chooses a character with a three-part condition — total cells = rows × cols.
Approach
How to Solve It
Nest loops over rows and columns. Compute mid, then append * or 0. End each row with console.log.
Method
Idea
Best for
Diagonals + mid
i===j / j===mid / anti-diagonal
Classic X demos
Diagonals only
Drop the j===mid check
Pure X contrast
Pseudocode
Pseudocode
mid = floor((cols + 1) / 2)
for i from 1 to rows:
line = ""
for j from 1 to cols:
if i == j or j == mid or i == cols + 1 - j:
append "*"
else:
append "0"
print line
Cheat sheet
Goal
Pattern
Walk grid
for (let i = 1; i <= rows; i++) / for (let j = 1; j <= cols; j++)
Center column
const mid = Math.floor((cols + 1) / 2);
Star test
i === j || j === mid || i === cols + 1 - j
Star / zero
line += "*"; / line += "0";
End the row
console.log(line);
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += …
Stays on the same row
Each cell (* or 0)
console.log(line)
Ends the current row
After all columns in the row
Append without a newline, then end the row once.
Try it
Live Preview
Change the row count (width becomes 2 × rows + 1) and the X updates instantly.
Whole numbers from 3 to 6. Columns = 2 × rows + 1 (odd width so the center column exists). Tap a chip or type a value — the preview redraws as you go.
Live result4 × 9 · 36 cells
*000*000*
0*00*00*0
00*0*0*00
000***000
Code
JavaScript Programs
Three complete programs: fixed 4×9, prompt size, and diagonals-only contrast. Use View Output for samples, or Try It Yourself to edit and run live.
Example 1 — Fixed rows = 4, cols = 9
Hard-coded size — diagonals plus center column of stars.
JavaScript
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);
}