







function printBoard(board) for (let i = 0; i < board.length; i++) console.log(board[i].join(" "));
A checkerboard alternates colors. If you look at the coordinates (row, col) :
Do not initialize the board like this: board = [[0]*8]*8 . This creates references to the same list 8 times. If you change one row, all rows change. Use a for loop to append new, unique lists to the board variable as shown in the code above. B. Accessing 2D Lists The core of this exercise is board[row][col] = 1 . board[0] accesses the first row (the first internal list). board[0][0] accesses the first element of the first row. C. The print_board Format
If your actual CodeHS prompt differs, tell me the exact statement and I will adapt this write-up.
: If you get a red mark saying "You should set some elements of your board to 1," ensure you are actually modifying or appending values to the list, not just printing text that looks like a grid. Function Placement : Always define your print_board function at the top of your script to avoid scope errors. for version 2 of this exercise? 9.1.6 checkerboard v1 codehs
Proof of correctness:
print_board(board)
: Ensure your loops use the strictly less than operator ( < NUM_ROWS ), not less than or equal to ( <= ). JavaScript arrays and grid iterations are zero-indexed.
Combining the steps above, here is the complete, functional code to solve the challenge on CodeHS, according to user discussions on Reddit : function printBoard(board) for (let i = 0; i < board
If you add the current row index ( r ) and column index ( c ), the sum alternates between even and odd numbers across the entire grid: Row 0, Col 0 Row 0, Col 1 Row 1, Col 0 Row 1, Col 1
The exercise asks you to create a grid representation of a checkerboard. In many programming challenges, a simple grid is represented as a list of lists. The goal is to fill an board where: Zeros ( ) represent empty spaces. Ones ( ) represent checker pieces.
is a foundational exercise designed to teach nested loops, 2D arrays (or grid systems), and conditional logic.
: Use a for loop to go through each row index ( i ) and column index ( j ). If you change one row, all rows change
Copy the code above, paste it into the CodeHS editor, and run it. You should see a perfect 8×8 checkerboard. If you run into issues, double-check your spelling of Color.GRAY (remember: American English spelling) and ensure you have imported acm.graphics.* and java.awt.* .
A checkerboard pattern relies on the parity of the coordinates. You can visualize the index sums like this: ✅ Final Result The program successfully generates an grid where every adjacent cell alternates between , starting with at position [0][0] .
# Pass this function a list of lists to print it as a grid def print_board ( board ): for i in range(len(board)): print( " " .join([str(x) for x in board[i]])) # 1. Start with an empty board and fill it with 0s board = [] for i in range( 8 ): board.append([ 0 ] * 8 ) # 2. Use nested loops to change 0s to 1s in the correct rows for i in range( 8 ): for j in range( 8 ): # Top 3 rows (0, 1, 2) and bottom 3 rows (5, 6, 7) if i < 3 or i > 4 : board[i][j] = 1 # 3. Print the final result print_board(board) Use code with caution. Copied to clipboard
Once all columns for a row are determined, row_list is appended to the board . After the loops complete, the finished 8x8 board is passed to the print_board function to display the output.
: If the sum leaves a remainder, it colors the square white . Visual Example Matrix (Row + Col) : Top-Left cell (0,0) : →right arrow Next cell right (0,1) : →right arrow First cell second row (1,0) : →right arrow Second cell second row (1,1) : →right arrow 3. Calculating Coordinates