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

Jessie Doherty
Jessie Doherty
7,571 Points

object literal question

Not so sure why my code for countWords() method doesn't work.

mystring.js
const myString = {
    string: "Programming with Treehouse is fun!"
  countWords() {
    return myString.string.split(' '); 
  }
}

1 Answer

Steven Parker
Steven Parker
231,236 Points

It looks like there are 3 issues:

  • the comma to separate the properties is missing
  • internal references to the object should use the keyword "this" instead of the object name
  • the function is returning an array of words instead of the number of words

The next video will show the teacher's solution with a full explanation.

Jessie Doherty
Jessie Doherty
7,571 Points

Thank you so much Steven for answering my question. I didn't realize that right after this challenge, Treehouse offered a solution. But your help is very much appreciated!!!