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

Fendy Azman
Fendy Azman
8,514 Points

Problem with returning number of char in string

The error says that my code doesn't return the correct numbers. Appreciate if you can pinpoint on the mistakes in my code. Thank you.

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

1 Answer

Steven Parker
Steven Parker
231,269 Points

You're right that this code is returning number of characters in the string, by using the length property.

But the instructions say that the method "should return the number of individual words in the string". You can assume that "words" are groups of of non-space characters separated by spaces.

Fendy Azman
Fendy Azman
8,514 Points

Yes, you're completely right. i've misread the question. I've learned the correct way by using the String.split() method and assigned the value in an array. Thank you.