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

Is this alternative solution reasonable?

This was my solution, though I modified my variable names to match the video.

I checked if the column was full first, to avoid unnecessary looping, by checking the first (upper most) space in the column. If it was empty, we know we can place the token in at least one space.

Then I set the game ready state to false.

Then I began looping through all of the spaces in the targetColumn, beginning from the last array index (bottom of column), to fill the lowest, open space first. Once it finds an open space it drops the token and then breaks out of the loop.

I wrote it this way thinking it would be more efficient, but also realize it may be less readable and imagine the performance difference is negligible in this app.

playToken() {
  const spaces = this.board.spaces;
  const activeToken = this.activePlayer.activeToken;
  const targetColumn = spaces[activeToken.columnLocation];

  if (!targetColumn[0].token) {
    this.ready = false;
    for(let i = targetColumn.length - 1; i > 0; i-- ) {
      if (!targetColumn[i].token) {
        activeToken.drop(targetColumn[i]);
        break;
      }
    }
  }
}