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

can my solution also work?

class Game {
    constructor() {
        this.board = new Board();
        this.players = this.createPlayers();
        this.ready = false;
    }

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

    createPlayers() {
        const players = [];

        let player1 = new Player('Player 1', '#e15258', 1, true);
        players.push(player1);

        let player2 = new Player('Player 2', '#e59a13', 2,);
        players.push(player2);

        return players;
    }
}

1 Answer

As far as I can see it will work but you declare array then you declare two variables and use push method twice. After some refactoring, you could do something simple like that.

class Game {
    constructor() {
        this.board = new Board();
        this.players = this.createPlayers();
        this.ready = false;
    }

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

    createPlayers() {
        return [ new Player('Player 1', '#e15258', 1, true),
                      new Player('Player 2', '#e59a13', 2) ];
    }
}