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 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
216,135 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.