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

Daniel Frisch
Daniel Frisch
14,713 Points

Complete the implementation of the merge method in utilities.js file. You should be able to pass in a string with placeh

I don't under stand what is missing.

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(mailValues, content) {
  for(var key in mailValues) {
    content = content.replace("%" + key + "%", mailValues[key]);
  }
  return content;
}


module.exports.merge = merge;

2 Answers

Seth Shober
Seth Shober
30,240 Points

You've mixed up your arguments. emailTemplate is the string you want to pass as content, and mailValues is the object you want to pass as mailValues.

I believe this is what you meant to do:

var mergedContent = utilities.merge(mailValues, emailTemplate);

Daniel Frisch
Daniel Frisch
14,713 Points

I figured it out. Thank you for trying to help out :) there was no need to change the argument names.

Seth Shober
Seth Shober
30,240 Points

Right on. What was the fix? When I ran your code I got an error because content was expecting a string and was passed an object. That's why I switched the order, which matched the function you had written, and it worked.

Daniel Frisch
Daniel Frisch
14,713 Points

I believe I did need to reverse the arguments in the merge function, but not the mergeContent variable. Also I didn't have to rename the content variable to say emailTemplate.

Seth Shober
Seth Shober
30,240 Points

cool. sounds like we both had different ways of thinking about the same thing.