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

Hi, I could not understand both of the diagonal parts

I can understand the columns and rows part but not the diagonal parts.

1 Answer

for the first diagonal check, start at point 3,0 middle column, bottom row and look left 1 (x-1) and up 1 (y+1), then look left 2 (x-2) and up 2 (y+2), then left 3 and up 3. Then change y to 1 and start at 3,1 and do the same checks. Then change y to 2 and start at 3,2 and do the same checks. Note that after moving up to y = 2, the 4th check is at 0,5, so the inner for loop is done and the outer loop is iterated, making the new starting position 4,0. Then it would start at 4,1, and 4,2 and repeats until the final starting position is 6,2. The second diagonal check works similarly, except that the first starting point is 3,3 and it checks diagonally left and down. The starting y coordinate is incremented until the top of the board, then x is incremented and y goes back to 3 and starts incrementing again. This repeats until the final starting position is 6,5 (the top right corner).