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

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Would this version of createPlayers() method work?

I think it's right but I need some reassurance :)

This is the version of the method I used as my answer for the createPlayers() method.

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

        //array of objects - initialised as empty array.
        player = []

        let playerOne = new Player("Player 1","#e15258", 1, true);
        let playerTwo = new Player("Player 2","#e59a13", 2);
        player.push(playerOne, playerTwo)
        return player;
    }

I think this is okay because led by the other examples in the code so far, I believe I'm still returning an array with each new instance of Player each getting their own variable.

is it any different to Ashley's version?

createPlayers() {
    const players = [new Player('Player 1', 1, '#e15258', true), new Player('Player 2', 2, '#e59a13')];
    returns players;

5 Answers

Aside from the fact that your player constructor seems to be different from hers, there is no difference. I don't really understand what your asking? Does it matter if you create your instances outside the array and 'push' them, or just initialize the array with the instances? No, why would it matter?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

So then, the answer is yes, it would work. Thanks for your response. I suspected it would be the case but I just thought I'd open up the code for "validation".

Ashley kind of expects that we do our own attempts but then make sure that our code follows along with hers. So what's the harm in getting involved with the community and asking the question. We're all here to learn. :-)

P.S. I'll amend my Player instances I didn't realise I'd got that in slightly the wrong order

That was how I did the method the first time around. After some refactoring I got it down to a single line:

return [new Player("Edward", 1, '#e15156', true), new Player("Fartbot 5k", 2, '#3e61e5')];

I did the same as you but i prefer the method in the solution video

Ian Ostrom
seal-mask
.a{fill-rule:evenodd;}techdegree
Ian Ostrom
Full Stack JavaScript Techdegree Student 10,331 Points

I did mine slightly different with the same result.

    createPlayers(){
        const player1 = new Player('Player 1', '#e152258', 1, true);
        const player2 = new Player('Player 2', '#e59a13', 2);
        return [player1, player2]
    }