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 Building Constructor Methods and Generating Objects Game Class Constructor Method Solution

Leonardo Motta
Leonardo Motta
11,284 Points

Would it work if I pass the board object as an parameter?

As you can see, the instructor created the new object board directly in the constructor this.board = new Board() and I was going to pass it as an argument const = new Game(new Board()); can this work?

class Game {
  constructor(board) {
    this.board = board;
    this.players = this.CreatePlayers;
    this.ready = false;
  }

  /**
   * Creates two player objects
   * @return  {Array}    An array of two Player objects.
   */

  CreatePlayers() {
    const players = [
      new Player("Player 1", 1, "#e15258", true),
      new Player("Player 2", 2, "#e59a13")
    ];

    return players;
  }
}

I do understand that her version is shorter and clean, just want to know if it would be valid code.

1 Answer

Tobiasz Gala
seal-mask
.a{fill-rule:evenodd;}techdegree
Tobiasz Gala
Full Stack JavaScript Techdegree Student 23,529 Points

It could be a good solution when you create game with specific number of rows and columns.

new Game(new Board(#ofrows, #ofcolumns));

But since this project creates game with specific number of rows and columns the code provided in the video is cleaner because you just call new game like that.

new Game(); // and everything is clean when you read the code - you start new game!