LoopsX Studio
No. 28 / Advanced

Checkerboard

Outputn = 5
* * *
 * * 
* * *
 * * 
* * *
5 rows
Controls
5
Character
Normal
How it works

An n × n grid. A cell is a star when i + j is even, so stars and spaces alternate in both directions.

rows ncolumns nstar when (i + j) % 2 == 0
pattern.js
1const n = 5;
2
3for (let i = 1; i <= n; i++) {
4 let row = "";
5 for (let j = 1; j <= n; j++) {
6 if ((i + j) % 2 === 0) {
7 row += "*";
8 } else {
9 row += " ";
10 }
11 }
12 console.log(row);
13}