Codehs - 9.1.7 Checkerboard V2

Introduction If you are currently working through the CodeHS Java (or JavaScript) curriculum , particularly the unit on Nested Loops or 2D Arrays , you have likely encountered the infamous exercise: 9.1.7 Checkerboard V2 .

1. Off-by-One Errors in Parity Wrong: if (row % 2 == 0) — This creates stripes, not a checkerboard. Fix: Always use (row + col) % 2 == 0 . 2. Hardcoding Dimensions The "V2" often implies a variable board size. If the autograder tests with 10x10 and your code only works for 8x8, you will fail. Fix: Use constants or user input at the top of your function. 3. Incorrect Starting Color If the expected output starts with a dark square at (0,0), ensure your if branch matches that. Swap colors if needed. 4. Graphical Version – Gaps Between Squares Using setFilled(true) alone might leave a tiny border. Some CodeHS exercises expect setFilled(true) without setColor for the outline. If gaps appear, set the outline color to match the fill color: 9.1.7 Checkerboard V2 Codehs

console.log(line);

public void run() double sqWidth = (double) getWidth() / NUM_COLS; double sqHeight = (double) getHeight() / NUM_ROWS; for (int row = 0; row < NUM_ROWS; row++) for (int col = 0; col < NUM_COLS; col++) double x = col * sqWidth; double y = row * sqHeight; GRect square = new GRect(x, y, sqWidth, sqHeight); square.setFilled(true); if ((row + col) % 2 == 0) square.setFillColor(Color.BLACK); else square.setFillColor(Color.RED); add(square); Introduction If you are currently working through the

Share by: