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 trialAlex Shah
5,420 PointsAdd 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) }
}
const myString = {
string: "Programming with Treehouse is fun!"
countwords:function(){
}
}
3 Answers
Ned Marafawi
7,995 PointsWhy 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?!
Florin Dumitrasc
7,771 PointsI had the same issue and came up with this solution, found on web:
return this.string.split(" ").length;
Steven Parker
231,248 PointsOddly, 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