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

Having to cheat online copy paste question into google Plus no workspaces available on this entire part of the course

Dont understand the question

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

2 Answers

This one is a tricky one, so I understand your confusion!

The question was : Add a method to the object literal called countWords(). countWords() should return the number of individual words in the string property.

Which means it wanted a method adding to the object - which you did correctly!- and it wants this method to return the number of words in the string property. To do this you must use the this keyword to access the current objects string property (again, you did this well :) ), and then use the split method to split the string at each space, then use the count method to count how many splits you end up with (which will be the amount of words in the string property)

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

Hope this helps.

I thought I was finding it difficult so my friend who has been a developer for 18 years says the teaching in many parts of treehouse isn't the best. Even at this point no teaching given on (this) whatever it is. plus treehouse lacks badly in amount of content given (variations of code examples).

I have to spend so much money on books and looking elsewhere to fills the gaps that i dont know why im still paying treehouse.

Given up on this practice session

If you're looking for good, interactive free code learning platforms i'd recommend W3Schools and freecodecamp. Freecodecamp also do videos, and lots of how tos that can help you solidify your knowledge through creating projects such as building websites from scratch or building online games. One of my favorite tutorials by them is building a chatbot with React

Thank you I am pulling hair out :-( very kind of you :-)