Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Object-Oriented JavaScript: Challenge Building Constructor Methods and Generating Objects createTokens() Method Solution

Could this method not be shorter?

I think Ashley included an unnecessary line of code when she put the instantiation of the Token object on a separate line. My code:

/**
     * Creates an array of Token objects for the player
     * @param {int} num - number of token objects to create
    */
    createTokens(num)
    {
        const tokens = new Array();
        for (let i = 0; i < num; i++) {
            output.push(new Token(i, this));
        }
        return tokens;
    }

Is this not more efficient?

1 Answer

Steven Parker
Steven Parker
221,451 Points

I doubt most of the course examples are meant to be taken as the best or only strategy for addressing a certain need. They are most likely picked just to clearly illustrate the use of the technique(s) being introduced.

The fact that you're thinking of effective alternative strategies is evidence of your learning progress and grasp of the material. Good job! :+1: