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 trialLily Peterson
10,685 PointsMerging files with Node.js
I can't seem to solve this particular problem. Here's what I'm supposed to do:
"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."
Here is the index.js file:
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 :)";
Here is the utilities.js file with the work I've already completed:
function merge(content, values) { for(var mailValues in values) { content = content.replace("{%" + mailValues + "%}", values[mailValues]); } return content; }
module.exports.merge = merge;
So, how do I solve this?
1 Answer
Doug Hawkinson
Full Stack JavaScript Techdegree Student 25,073 PointsI know this was way too far back to expect that you still need an answer, but as far as I can tell you never got one, so... I'm answering because it messed with me too.
function merge(content, values) {
for(var mailValues in values) {
content = content.replace("{%" + mailValues + "%}", values[mailValues]);
}
return content;
}
Change "{%" and "%}" to "%"