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

Devin Scheu
Devin Scheu
66,191 Points

JavaScript Help

Question: Complete the implementation of the merge method in utilities.js file. You should be able to pass in a string with placeholders with percent signs (%) surrounding them. The second parameter should be an object with values to be inserted in to the placeholders. Look at index.js to see how it should work.

Code:

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[values]);
  return content;
}


module.exports.merge = merge;

6 Answers

Daniel Johnson
Daniel Johnson
104,132 Points

You were close, but you forgot the for loop needed for your values and your replacement was slightly off...

utilities.js
function merge(content, values) {
  for (key in values) {
    content = content.replace("%"+key+"%", values[key]);
  }
  return content;
}

module.exports.merge = merge;
Devin Scheu
Devin Scheu
66,191 Points

Thanks Daniel, Congrats by the way on taking number 1!

Charley Montgomery
Charley Montgomery
9,372 Points

Why does this require a for loop? I understand that there may be more than one person in an object but for the purposes of the challenge it seems like simply replacing the string with the required object's first name property would be sufficient.

Carlo Antonio Bilbao
Carlo Antonio Bilbao
24,113 Points

This question was a bit ambiguous. I although thought we were simply replacing just one item. There was no indication that a loop would be needed since there was only one item in the object. The directions also didn't suggest that there may be more items in the object.

thomas howard
thomas howard
17,572 Points

I was confused, and still am, about the question. I could get the answer simply from reviewing this. I'm amazed that it was from people with a billion points. Is this stuff hard? It's not hard. Most of us are just working day jobs and don't have the luxury of hanging out at a code shop for work all day, right?

That is because we have smart programmers teaching us, whom don't understand HOW to teach... Dave McFarland is the only teacher worth learning from.. I never had an issue with a quiz or challenge from him because he is thorough and teaches to every line he writes. Plus the challenges are relevant to what we learned.. Some other teachers are good but this guy... hmmm

Congrats Daniel and devin on your high scores :)

so....this would have been easier had there also been an index.html to look at as well, thanks for the help, i would have been so confused had someone not asked this question!