9.1.7 Checkerboard V2 Codehs Free Access
: Iterate through the rows and columns. Use an if statement with the modulo operator to check the indices.
# This is the logic for the checkerboard pattern: # If the sum of the row and column indices is even, make it red. # Otherwise, make it black. if (row + col) % 2 == 0: pen.color("red") else: pen.color("black") 9.1.7 Checkerboard V2 Codehs
Create an empty list and use a loop to append 8 sub-lists, each containing eight zeros. : Iterate through the rows and columns
function start() for (var row = 0; row < NUM_ROWS; row++) for (var col = 0; col < NUM_COLS; col++) var x = col * SQUARE_SIZE; var y = row * SQUARE_SIZE; var color; if ((row + col) % 2 == 0) color = "black"; else color = "red"; # Otherwise, make it black
In programming, grids almost always start at index 0 . Use row < 8 .
In the exercise on CodeHS, you create a checkerboard pattern by utilizing a for loop to iterate through a 2D array (grid) and assigning colors based on whether the sum of the row and column indices is even or odd. 1. Initialize the grid