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 Practice Object Basics in JavaScript Practicing Object Basics Practice Adding a Property on the Fly

Please be clear what is being asked in this challenge...

What I'm frustrated about here is there are conflicting statements. Am I supposed to add a variable equal to this? A property within this object? A separate object? This is so incredibly unclear. This is the second time within this set that this "instructor" has done this, she needs to be clear and concise in her words/terminology. This is such a waste of time, bring back Guil.

1 Answer

Hi Andrew,

While I didn’t find the statements within the challenge to be conflicting, it did take me a second to process what it wanted me to do.

I’m assuming you know what an object literal is. If not (if you’re shaky on that), then that would definitely throw the objective for the challenge out of whack. Anyway, the challenge is asking you to create a property outside of the object. So don’t mess with the code for the object directly, but add a property.

And there are two ways in which you can add a property (or method) to an object. Either through dot or bracket syntax.

So... someObject.myProperty = ‘foo’
Or... someObject[‘myProperty’] = ‘foo’

Here’s a solution:

const myString = {
    string: "Programming with Treehouse is fun!",
    countWords: function(){
        const wordArray = this.string.split(' ');
        return wordArray.length;
    }
}

myString.characters = myString.string.length // my addition

var numWords = myString.countWords();