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 Build a Simple Dynamic Site with Node.js Creating a Basic Template Engine in Node.js A Simple Merge Utility

jason limmm
jason limmm
8,009 Points

i need help

there is a problem with my code that i'm not sure how to explain... this is from a challenge and my message should refer to the challenge hoppefully

index.js
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 :)";
utilities.js
function merge(content, values) {
  content=content.replace("%"+values+"%");
  return content;
}


module.exports.merge = merge;

1 Answer

Steven Parker
Steven Parker
231,186 Points

The instructions tell you that "The second parameter should be an object with values to be inserted…", so it can't be used as a string. And you can see from the sample index.js code that the object will have properties that identify where the value will be substituted.

If you create a loop that will iterate through all the properties of the object, you can use the property names to create the strings to identify what to replace with the associated value.

Also, don't forget that the replace method takes two arguments, the pattern and the string to replace it with.

Finally, if you want your method to handle cases where a token is used more than once, you could use replaceAll instead.