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 trialSam Weeks
Front End Web Development Techdegree Student 16,699 PointsHow is this code returning the incorrect value for the code challenge?
myString.characters = function () {
return this.string.length
};
const myString = {
string: "Programming with Treehouse is fun!",
countWords: function(){
const wordArray = this.string.split(' ');
return wordArray.length;
}
}
var numWords = myString.countWords();
myString.characters = function () {
return this.string.length
};
1 Answer
Steven Parker
231,275 PointsI see two issues:
- the instructions ask just for a property, not a new method/function
- you cannot use "this" outside of the object, reference the object by name instead
Sam Weeks
Front End Web Development Techdegree Student 16,699 PointsSam Weeks
Front End Web Development Techdegree Student 16,699 PointsI see what your saying with the first issue however when i ran this via the console it returned the same value i.e
myString.character = myString.string.length
: returns 34 and the code I used above when loggingmyString.character()
returns 34Steven Parker
231,275 PointsSteven Parker
231,275 PointsThe challenge is looking for you to create the property containing a value as the instructions ask. The function may generate the same value, but type of the property will be different.
For best results with the challenges, always do what the instructions ask for!