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

Sam Weeks
seal-mask
.a{fill-rule:evenodd;}techdegree
Sam Weeks
Front End Web Development Techdegree Student 16,699 Points

How is this code returning the incorrect value for the code challenge?

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


var numWords = myString.countWords();

myString.characters = function () {
  return this.string.length
}; 

1 Answer

Steven Parker
Steven Parker
230,178 Points

I see two issues:

  • the instructions ask just for a property, not a new method/function
  • you cannot use "this" outside of the object, reference the object by name instead
Sam Weeks
seal-mask
.a{fill-rule:evenodd;}techdegree
Sam Weeks
Front End Web Development Techdegree Student 16,699 Points

I see what your saying with the first issue however when i ran this via the console it returned the same value i.e myString.character = myString.string.length: returns 34 and the code I used above when logging myString.character() returns 34

Steven Parker
Steven Parker
230,178 Points

The challenge is looking for you to create the property containing a value as the instructions ask. The function may generate the same value, but type of the property will be different.

For best results with the challenges, always do what the instructions ask for!