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 trialNathaniel Saludes
8,345 PointsThe challenge says that my implementation is off but when I tested it locally using node.js, it works anyway.
I did everything it says on the challenge and tested my code locally (which works) but it doesn't seem to accept it.
var utilities = require("./utilities.js");
var mailValues = {};
mailValues.first_name = "Janet";
var emailTemplate = "Hi %first_name%! Thanks for completing this code challenge :)";
var mergedContent = utilities.merge(emailTemplate, mailValues);
//mergedContent === "Hi Janet! Thanks for completing this code challenge :)";
function merge(content, values) {
content = content.replace('%first_name%', values.first_name);
return content;
}
module.exports.merge = merge;
2 Answers
Steven Parker
231,275 PointsI can see that your code will handle the sample that only replaces "first_name", but the challenge is asking for a function that would be able to handle larger value sets with different token names as well. I'm sure the validator is testing it with different data from the sample.
Hints: You'll probably need a loop, and don't hard-code any property names.
Thomas Tilton-Heylin
12,098 Pointsfunction merge(content, values) {
for(var key in values) {
// replace all key with the value from the values object
content = content.replace("%" + key +"%", values[key]);
}
return content;
}
module.exports.merge = merge;
this will complete the challenge. I wish they always had challenge completions up so you can actively double check your work must faster. There is technically no cheating on treehouse.
Steven Parker
231,275 PointsWell, it may not be "cheating", but they also strongly discourage posting cut-and-paste-able solutions without at least a detailed explanation of how it works. I've seen some cases where such a posting was redacted by staff member or moderator.
Nathaniel Saludes
8,345 PointsNathaniel Saludes
8,345 PointsIt worked! thanks for the hint... I made a mistake by assuming that it will only test it with the given object property