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 Rendering the Game Getter Methods Solution

shaun bolak
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
shaun bolak
Front End Web Development Techdegree Graduate 18,080 Points

OOJS challenge - Would this work for the activePlayer() method?

Ashley's code for this method is:

    activePlayer() {
        return this.players.find(player => player.active);
    }


The code I came up with, prior to seeing Ashley's more eloquent solution ; ) :

    activePlayer() {
        return this.players.filter(active = true);
    }

Do they both produce the same results (a string value like "player1")? Please let me know if you have time. Cheers^^

1 Answer

Steven Parker
Steven Parker
231,236 Points

First, a single "=" is an assignment, not a comparison. But the syntax of the argument to "filter" should be similar to the argument you would give to "find".

The big difference is what they do: "find" returns just one element but "filter" returns a whole array of every element that meets the test criteria.

shaun bolak
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
shaun bolak
Front End Web Development Techdegree Graduate 18,080 Points

Hi Steven. Thank you for your reply. I got a big laugh out of the first sentence that you wrote - what a thing to miss lol! Your answer makes sense. I will go with Ashley's code.