JavaScript Practice Object Basics in JavaScript Practicing Object Basics Practice Adding a Method to an Object Literal

Umar Ijaz
seal-mask
.a{fill-rule:evenodd;}techdegree
Umar Ijaz
Full Stack JavaScript Techdegree Student 6,944 Points

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
4,069 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

Nathan Stahlman
seal-mask
.a{fill-rule:evenodd;}techdegree
Nathan Stahlman
Full Stack JavaScript Techdegree Student 14,391 Points

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!