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

Arthur Berg
Arthur Berg
11,350 Points

Hey i'm having problem with my utilities file in node.js challenge. Help please!

Hey. I'm having problem with writing out the content properly in my mergedContent function. First day with Node.js and i'm a bit confused, some help and explanation would be gold. Thanks!

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("%first_name%", values.first_name)
  return content;
}


module.exports.merge = merge;

1 Answer

Steven Parker
Steven Parker
229,732 Points

This function will work, but only for the sample template. The challenge is to create a function that would work with any template, and replace any number of keys with their values. (And it tests it using different arguments than the sample).

Think about how you could make this function work without knowing in advance how many keys are in the template or values, or what their names are.

Hints: you'll need to build your replace pattern up using string concatenation, and a loop to iterate through all the keys that may be in values.

If you're still stuck, you could look at this question asked by another student that I answered just this morning. But give it another try first, that other post would be a spoiler.