V2 Codehs — 9.1.7 Checkerboard

What does the grid store? (Integers, Strings, Colors?) Share public link

function start() var boardSize = 8; var squareSize = 50; var colors = ["red", "black"]; for(var i = 0; i < boardSize; i++) for(var j = 0; j < boardSize; j++) var square = new Rectangle(squareSize, squareSize); square.setPosition(j * squareSize, i * squareSize); square.setColor(colors[(i + j) % 2]); add(square);

The objective of Checkerboard V2 is to generate a grid pattern where alternating squares display contrasting colors. Unlike basic grid exercises, Version 2 often demands that your code handles dynamic grid sizes or specific functional breakdowns using nested loops and conditional statements. Key Mechanics:

By adding the row index and column index (i + j) , you create a diagonal wave of even and odd numbers: 0+0 = 0 ( Even ) → 1 Row 0, Col 1: 0+1 = 1 ( Odd ) → 0 Row 1, Col 0: 1+0 = 1 ( Odd ) → 0 Row 1, Col 1: 1+1 = 2 ( Even ) → 1 Alternative Approach: Even vs Odd Rows You can also think about it row by row: Even rows (0, 2, 4...) start with 1 . Odd rows (1, 3, 5...) start with 0 . ⚠️ Common Pitfalls in CodeHS 9.1.7 Checkerboard V2 Codehs

Forgetting to add r + c together inside the modulo check. If you only check c % 2 == 0 , you will end up with vertical stripes. If you only check r % 2 == 0 , you will get horizontal stripes.

import turtle

// Assuming a grid of integers where 1 represents black and 0 represents red int[][] checkerboard = new int[8][8]; for (int row = 0; row < checkerboard.length; row++) for (int col = 0; col < checkerboard[row].length; col++) // Check if the sum of current row and column indices is even if ((row + col) % 2 == 0) checkerboard[row][col] = 1; // Element A else checkerboard[row][col] = 0; // Element B Use code with caution. What does the grid store

: In Python, improper indentation inside nested loops is the most frequent cause of "Syntax Error" or incorrect patterns. Hardcoding

A 2D list is essentially a list containing other lists. It tracks coordinates through standard [row][column] indexing syntax. : Accesses the entirety of the first horizontal row.

Start by defining your board dimensions and colors. This makes your code modular and easy to change later. GRID_SIZE : The total number of rows and columns. Key Mechanics: By adding the row index and

After the loops have finished building your board, you must print it row by row. Using print " ".join(row)

# After finishing a row, move down to the start of the next row pen.backward(square_size * 8) # Return to the left side pen.right(90) # Turn down pen.forward(square_size) # Move down one row pen.left(90) # Turn back to facing right