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 Build the checkForWin() Method

Any feedback on my attempt would be great. Not sure If it would work or how I could improve upon it. (inside board.js)

win() { console.log('win') } checkWin() { const spaces = this.spaces const cols = this.cols const rows = this.rows

    for (let col of spaces) {
        for (let focus of col) {
            let x = focus.x
            let y = focus.y

    // horizontal check 
            if ( x + 3 < cols ) {
                for (let i = 1; i < 4; i++) {
                    let trueArray = []
                    let spacecheck= spaces.find(space => x === x+i && space.y === y)
                    if (focus.owner === spacecheck.owner) {
                        trueArray.push('T')
                    }
                    if (trueArray.length === 3) { this.win() }
                }
            }

    // verticle check 
            if ( y + 3 < rows ) {
                for (let i = 1; i < 4; i++) {
                    let trueArray = []
                    let spacecheck= spaces.find(space => space.x === x && space.y === y+i )
                    if (focus.owner === spacecheck.owner) {
                        trueArray.push('T')
                    }
                    if (trueArray.length === 3) { this.win() }
                }
            }

    // diagonal down-right check 
            if ( x + 3 < cols  && y + 3 < rows) {
                for (let i = 1; i < 4; i++) {
                    let trueArray = []
                    let spacecheck= spaces.find(space => space.x === x+i && space.y === y+i)
                    if (focus.owner === spacecheck.owner) {
                        trueArray.push('T')
                    }
                    if (trueArray.length === 3) { this.win() }
                }
            }
    // diagonal down-left check
            if ( x >= 0  && y >= 0) {
                for (let i = -1; i > -4; i -= 1) {
                    let trueArray = []
                    let spacecheck= spaces.find(space => space.x === x+i && space.y === y+i)
                    if (focus.owner === spacecheck.owner) {
                        trueArray.push('T')
                    }
                    if (trueArray.length === 3) { this.win() }
                }
            }
        }    
    }
}