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 trialcdlvr
14,448 PointsWhy use Array.filter() instead of Array.find() to return a single, unused Token?
The solution has us create a get unusedTokens() method that filters the list of tokens down to only the unused tokens:
get unusedTokens() {
return this.tokens.filter(token => !token.dropped);
}
Then grabs the first token from the unused token to use as the active token:
get activeToken() {
return this.unusedTokens[0];
}
Is there a reason for doing this rather than using Array.find() to grab an unused token in a single step? i.e.:
get activeToken() {
return this.tokens.find(token => !token.dropped);
}
1 Answer
Steven Parker
231,236 PointsOne reason to implement them separately might be if there was another use for the unused tokens list elsewhere in the code (or if one was anticipated in the future). But it might also just be that "find" had not been introduced in the course.