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 trialjason limmm
8,009 Pointsi need help
there is a problem with my code that i'm not sure how to explain... this is from a challenge and my message should refer to the challenge hoppefully
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+"%");
return content;
}
module.exports.merge = merge;
1 Answer
Steven Parker
231,198 PointsThe instructions tell you that "The second parameter should be an object with values to be insertedβ¦", so it can't be used as a string. And you can see from the sample index.js code that the object will have properties that identify where the value will be substituted.
If you create a loop that will iterate through all the properties of the object, you can use the property names to create the strings to identify what to replace with the associated value.
Also, don't forget that the replace method takes two arguments, the pattern and the string to replace it with.
Finally, if you want your method to handle cases where a token is used more than once, you could use replaceAll instead.