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 Method to an Object Literal

Alex Shah
Alex Shah
5,420 Points

Add a method to the object literal called countWords(). countWords() should return the number of individual words in the

const myString = { string: "Programming with Treehouse is fun!" countwords:function(){ countWords(myString.length) }

}

mystring.js
const myString = {
    string: "Programming with Treehouse is fun!"
  countwords:function(){

  }

}

3 Answers

Why I got the challenge passed, I'm confused..

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

After watching the solution, my code is way off and it shouldn't pass, right?!

I had the same issue and came up with this solution, found on web:

return this.string.split(" ").length;

Steven Parker
Steven Parker
229,732 Points

Oddly, some of the code shown in your question didn't show up in the formatted code sample. But based on what's in the question:

  • object properties must be separated by a comma
  • "countwords" should be spelled "countWords" (with a capital "W")
  • the object should not reference itself by name
  • the method should not call itself
  • the method should return a value