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

What counts as a word?

Hello All,

I am working on this code challenge and want to make sure I have the understanding of the problem right.

We are supposed to only count words in the string, that is, a-z and A-Z characters only??? So spaces, punctuation, and numbers appearing in the value of the string property aren't counted?

I am trying to use a regex and the replace method and then the length property of the resulting string (with punctuation, numbers, and white spaces removed) to tackle this problem.

mystring.js
const myString = {
    string: "Programming with Treehouse is fun!",

  countWords: function() {
                return this.string.replace(/[^a-zA-Z!]/g,'').length; 
             }
}
Simon Coates
Simon Coates
8,377 Points

I had a quick look and most users just split on space. For instance, https://teamtreehouse.com/community/im-stuck-on-this-code . Possibly regex might be better. I mean splitting on " " is going to trip up if a user accidentally uses multiple spaces between words or runs words together.

1 Answer

Hi, Umar. I believe you are correct in that a word is a-z, no spaces. For example, your string, "Programming with Treehouse is fun!", has 5 words. That is all I can offer without seeing your specific instructions. Hope that helps!

Hi Nathan. Thanks, this helps; figured it out!