ALStrive

916 Checkerboard V1 Codehs | Fixed

Here is the fully functional Java code to pass the CodeHS autograder.

This is the "aha!" moment for the assignment. It teaches that patterns in computer science are often mathematical. By checking if the sum of the coordinates is even or odd, the code automatically creates the staggered pattern required for a checkerboard, regardless of the grid size. 916 checkerboard v1 codehs fixed

// Use nested loops to generate the rows and columns for (var row = 0; row < BOARD_SIZE; row++) for (var col = 0; col < BOARD_SIZE; col++) // Calculate the x and y coordinates for the square's top-left corner var x = col * SQUARE_SIZE; var y = row * SQUARE_SIZE; Here is the fully functional Java code to

function start() // Set up canvas var WIDTH = 400; var HEIGHT = 400; var ROWS = 8; var COLS = 8; var squareSize = WIDTH / ROWS; // Nested loops to create the board for(var row = 0; row < ROWS; row++) for(var col = 0; col < COLS; col++) var x = col * squareSize; var y = row * squareSize; var rect = new Rectangle(squareSize, squareSize); rect.setPosition(x, y); By checking if the sum of the coordinates

for (var i = 0; i < 8; i++) var row = ""; for (var j = 0; j < 8; j++) row += board[i][j] + " ";

The autograder often fails if you simply print "1" or use a shortcut. To pass, you must: Initialize an 8x8 grid filled with 0 . Loop through the rows and columns. Update specific elements to 1 using board[i][j] = 1 . Fixed Python Code

Using Python, you can test if a number is even by seeing if the remainder is 0 when divided by 2. The logic translates to this exact conditional expression: