Checkerboard V2 Codehs - 9.1.7
Using a loop inside another loop to handle the rows ( -axis) and columns ( -axis) of the grid.
This problem is a classic introduction to and Modular Arithmetic . It asks you to draw a checkerboard pattern where the color of each square depends on its position (row and column).
If (row + column) % 2 == 0 → Color A. If (row + column) % 2 == 1 → Color B.
For the next row, Modulo knew it couldn't be the same, or the colors would touch. He told the second apprentice, "Start with pearl (1) instead, then obsidian (0). Repeat that four times."The second apprentice laid out: [1, 0, 1, 0, 1, 0, 1, 0] . 3. Automating the Entire Floor 9.1.7 Checkerboard V2 Codehs
In the graphics version, recompute square size on window resize. This is rare for CodeHS but possible in advanced sections.
add(square);
The Checkerboard V2 project builds upon the foundational concepts of grid-based design and pattern creation. Students are tasked with writing a program that generates a checkerboard pattern consisting of alternating black and white squares, arranged in an 8x8 grid. The pattern should exhibit specific characteristics, such as: Using a loop inside another loop to handle
Since I can't see your specific assignment screen, I'll provide a general solution and explanation for drawing a checkerboard pattern, which is a common exercise in CodeHS's JavaScript Graphics unit.
Square Positioning: Remember that the x-coordinate is based on the column index multiplied by the square width, and the y-coordinate is based on the row index multiplied by the square height.
var rect = new Rectangle(size, size); rect.setPosition(x, y); If (row + column) % 2 == 0 → Color A
: The (r + c) % 2 trick is the industry standard for creating grid patterns—it’s much cleaner than using multiple if/else statements for odd/even rows.
Show how to do this in if you are in the Nitro/Java course
: Do not hardcode the loop limits as 8 if the exercise provides variable inputs. Always use board.length for rows and board[r].length for columns to keep the code dynamic.
To create a checkerboard, we use the row and column indices. If the sum of the and column index is even, we assign one value (e.g., 0); if it is odd, we assign the other (e.g., 1). This is easily checked using the modulo operator ( % ): if (row + col) % 2 == 0: (Sum is even) else: (Sum is odd) Step-by-Step Implementation