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 trialUmar Ijaz
Courses Plus Student 7,278 PointsWhat 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.
const myString = {
string: "Programming with Treehouse is fun!",
countWords: function() {
return this.string.replace(/[^a-zA-Z!]/g,'').length;
}
}
1 Answer
Nathan Stahlman
Full Stack JavaScript Techdegree Graduate 18,351 PointsHi, 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!
Umar Ijaz
Courses Plus Student 7,278 PointsHi Nathan. Thanks, this helps; figured it out!
Simon Coates
8,377 PointsSimon Coates
8,377 PointsI 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.