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

Richard Eldridge
Richard Eldridge
8,229 Points

Is this valid?

As much as I appreciate the succinctness of the solution code, and I'm always down for learning new methods, I'm still in a place where I'm trying to cement my understanding of the basics. On that vein, I was wondering if the following would perform the same as the .filter() method mentioned in the solution:

    get unusedTokens(){
        const tokensRemaining = [];
        for (let i = 0; i < this.tokens.length; i++) {
            if (this.tokens[i].token.dropped !== false) {
                tokensRemaining.push(this.tokens[i]);
            }
        }
        return tokensRemaining;
    }

    get activeToken(){
        return this.unusedTokens[0];
    }

I'm not sure if this.tokens[i].token.dropped is a thing. Plus I'm not sure if creating a new variable tokensRemaining is necessary. I feel like it might interfere with the activeToken() getter method. I'm so lost...

1 Answer

Steven Parker
Steven Parker
231,275 Points

Your idea is sound, but you have an extra "token" in there. Also, you don't need to compare booleans, they are either "true" or "false" already. And you want the items that are not dropped. So you might recode your "if" statement like this:

            if (!this.tokens[i].dropped) {

With those fixes, this would be one of the equivalent methods that the instructor mentioned.

And yes, you need the new array to do it this way so you have something to push the selected items onto.

Richard Eldridge
Richard Eldridge
8,229 Points

Thanks so much! Helps with knowing my level of understanding if I can solve a problem a different way.