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 trialkoen haentjens
7,343 PointsI me stuck on this one ...
keep getting content.replace is not a function
var utilities = require("./utilities");
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 :)";
var fs = require("fs");
function merge(values, content) {
//Cycle over the keys
for(var key in values) {
//Replace all {{key}} with the value from the values object
content = content.replace(/%+key+%/g, values[key]);
}
//return merged content
return content;
}
module.exports.merge = merge;
1 Answer
Steven Parker
231,269 PointsReplace is a string function; but you seem to have swapped the order of the arguments to merge, causing content to not be the string. It's important to preserve the original order so that content comes first, and values second.
Also, you can't concatenate literal strings and variables without putting the literals in quotes. Also, string concatenation only works on strings, not regexes. You can still do what you want, but you need to explicitly create the regex by calling its constructor to convert the concatenated string into a regex:
content = content.replace(new RegExp('%'+key+'%', 'g'), values[key]);