9.1.7 Checkerboard V2 Codehs Patched -
The outer loop for r in range(NUM_ROWS): starts at row 0. Before moving to row 1, it hands control to the inner loop for c in range(NUM_COLS): . The inner loop runs completely across all columns. This means the program draws the board left-to-right, top-to-bottom. 3. Coordinate Positioning
First, create a variable to hold your grid. You will start with an empty list and then append rows to it. In CodeHS, you are typically expected to build an structure. 2. Create the Nested Loops You need two loops: one for the rows and one for the columns. outer loop iterates through the row indices ( inner loop iterates through the column indices ( 3. Apply the Alternating Pattern Inside the nested loop, check the sum of the current . Use the modulus operator to check for even or odd values. if (row + col) % 2 == 0 If true, set the cell to If false, set the cell to 4. Print the Result
To build a checkerboard pattern efficiently, you do not need to keep track of a toggle variable that manually flips back and forth. Manual toggling often introduces bugs when switching to a new row. Instead, you can rely on the mathematical properties of coordinates. 9.1.7 Checkerboard V2 Codehs
: The CodeHS autograder often checks for an "assignment statement" (e.g., grid[i][j] = 1
Start by defining the size of the board and the squares. Using constants makes your code easy to modify later. javascript The outer loop for r in range(NUM_ROWS): starts at row 0
Here is a story that illustrates the logic behind this coding task through a real-world analogy. The Story: The Grand Tile-Setter's Strategy
If COLS were odd, this extra toggle wouldn’t be necessary, but here it is. This means the program draws the board left-to-right,
checkerboard of obsidian and pearl tiles. However, Modulo was notoriously lazy and wanted a single set of instructions his apprentices could follow without him being there. 1. Designing the First Row