V1 Codehs | 9.1.6 Checkerboard

), creating all 64 squares of a standard chess or checkerboard. 3. The Alternating Color Logic (The Modulo Trick)

# This function is provided to help you visualize your board def print_board(board): for row in board: print(" ".join([str(x) for x in row]))

To create the in CodeHS, you need to use nested for loops to place circles in a grid pattern . 🏁 Core Logic The goal is to create an grid of circles. The outer loop controls the rows (vertical movement). The inner loop controls the columns (horizontal movement). The spacing is determined by the radius of the circle. 💻 Solution Code javascript 9.1.6 checkerboard v1 codehs

The solution demonstrates how to create an alternating pattern without knowing the grid size in advance, an essential concept in robotics and grid-based programming.

The key to identifying a "checkerboard" pattern is the relationship between the ( ) and the column index ( A cell belongs to one "color" if the sum of its indices ( ) is even . ), creating all 64 squares of a standard

This exercise focuses on using nested loops modulus operator

function start() // Define the size of the board var NUM_ROWS = 8; var NUM_COLS = 8; // Outer loop handles the rows for (var row = 0; row < NUM_ROWS; row++) // Inner loop handles the columns for (var col = 0; col < NUM_COLS; col++) // Check if the sum of row and col is even if ((row + col) % 2 == 0) drawSquare(row, col, Color.red); else drawSquare(row, col, Color.black); function drawSquare(row, col, color) var sideLength = getWidth() / 8; var x = col * sideLength; var y = row * sideLength; var rect = new Rectangle(sideLength, sideLength); rect.setPosition(x, y); rect.setColor(color); add(rect); Use code with caution. Key Components Explained 1. Nested Loops 🏁 Core Logic The goal is to create an grid of circles

Does your canvas require instead of the standard red and black? Do you need help adapting this code for Checkerboard v2 ? Share public link

// Optional: return to start turnAround(); while (frontIsClear()) move();