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 trialDevin Scheu
66,191 PointsJavaScript 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:
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 :)";
function merge(content, values) {
content = content.replace("%", values[values]);
return content;
}
module.exports.merge = merge;
6 Answers
Daniel Johnson
104,132 PointsYou were close, but you forgot the for loop needed for your values and your replacement was slightly off...
function merge(content, values) {
for (key in values) {
content = content.replace("%"+key+"%", values[key]);
}
return content;
}
module.exports.merge = merge;
Charley Montgomery
9,372 PointsWhy 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
24,113 PointsThis 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
17,572 PointsI 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?
David Ryan
Courses Plus Student 14,981 PointsThat 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
ellie adam
26,377 PointsCongrats Daniel and devin on your high scores :)
Daniel Johnson
104,132 PointsThanks Ellie! :)
Devin Scheu
66,191 PointsThank You Ellie! :D
Aaron Endsley
Courses Plus Student 17,625 Pointsso....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!
Devin Scheu
66,191 PointsDevin Scheu
66,191 PointsThanks Daniel, Congrats by the way on taking number 1!
Daniel Johnson
104,132 PointsDaniel Johnson
104,132 PointsThanks! :)