Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Build a Simple Dynamic Site with Node.js Creating a Basic Template Engine in Node.js A Simple Merge Utility

I 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
Steven Parker
217,602 Points

Replace 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]);