Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Object-Oriented JavaScript: Challenge Adding the Game Logic checkForWin() Method Challenge Solution

Is there an error in this code? I think 4 should be subtracted from no of columns and rows in the loop conditions.

Since there are 7 columns and 6 rows, I believe that we should subtract 4 (not 3) in order to make the loop iterate properly. 7-4=3, and sequence of 3,4,5,6 horizontally is the final one, since there are not enough columns for 4,5,6,7. (we count columns starting with 0 value. the same applies to rows I recon.)

3 Answers

I don't really understand your argument but,

The loop iterates 3 times from 0,1,2

For instance this is the iteration for the first column

//1 iteration
if (this.board.spaces[0][0].owner === owner && 
                    this.board.spaces[0][0+1].owner === owner && 
                    this.board.spaces[0][0+2].owner === owner && 
                    this.board.spaces[0][0+3].owner === owner) {
                        win = true;
                    }    

//2 iteration
if (this.board.spaces[0][1].owner === owner && 
                    this.board.spaces[0][1+1].owner === owner && 
                    this.board.spaces[0][1+2].owner === owner && 
                    this.board.spaces[0][1+3].owner === owner) {
                        win = true;
                    }    

//3 iteration
if (this.board.spaces[0][2].owner === owner && 
                    this.board.spaces[0][2+1].owner === owner && 
                    this.board.spaces[0][2+2].owner === owner && 
                    this.board.spaces[0][2+3].owner === owner) {
                        win = true;
                    }    

On the 3rd iteration there're exactly 4 rows left, which is required to have 4 in a row.

1 OOOOOOO //  [0][0]
2 OOOOOOO // [0][1]
3 OOOOOOO // [0][2] <- last iteration / check for 4 rows
4 OOOOOOO // [0][3]
5 OOOOOOO // [0][4]
6 OOOOOOO // [0][5]
Steven Parker
Steven Parker
231,269 Points

The reason for subtracting 3 is that the code body adds up to 3 when checking the other cells. This makes sure that the last cell on the grid will be checked but that the loop will not go outside the grid.

If you subtract 4 instead, the last cell in the grid won't get checked.

Thank you for detailed explanation - I marked Steve for best answer already - but cheers anyway

Steven Parker
Steven Parker
231,269 Points

Marcin Lipski — Try marking it again. Once chosen, a "best answer" will have a light green background, and a green check-mark will appear next to the question in the forum index.