V1 Codehs Fixed [cracked] - 916 Checkerboard
Each square must be the width of the canvas divided by 8.
If you are working through the CodeHS curriculum, you’ve likely encountered the assignment. It’s a classic challenge that tests your ability to use nested loops, coordinate systems, and conditional logic. 916 checkerboard v1 codehs fixed
Are you having trouble with the version of this assignment, or is the autograder still giving you a specific error message? Each square must be the width of the canvas divided by 8
var SQUARES_PER_SIDE = 8; var SQUARE_SIZE = getWidth() / SQUARES_PER_SIDE; function start() { for (var row = 0; row < SQUARES_PER_SIDE; row++) { for (var col = 0; col < SQUARES_PER_SIDE; col++) { drawSquare(row, col); } } } function drawSquare(row, col) { var x = col * SQUARE_SIZE; var y = row * SQUARE_SIZE; var rect = new Rectangle(SQUARE_SIZE, SQUARE_SIZE); rect.setPosition(x, y); // The "Fixed" Logic: Check if sum of indices is even if ((row + col) % 2 == 0) { rect.setColor(Color.red); } else { rect.setColor(Color.black); } add(rect); } Use code with caution. Troubleshooting Common Errors 1. The "Off-by-One" Pixel Gap var SQUARE_SIZE = getWidth() / SQUARES_PER_SIDE
The color must switch based on both the row and column index to create the staggered effect. The Logic Behind the Fix
Here is a clean, "fixed" implementation for the CodeHS environment: javascript