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

My code is working in browser correctly why is it not working in the threehouse?

<!DOCTYPE html> <html> <body>

<h2>My First JavaScript</h2>

<button type="button" onclick="document.getElementById('demo').innerHTML = Date()"> Click me to display Date and Time.</button>

<p id="demo"></p>

<script>

var mailValues = {};

mailValues.first_name = "Janet";

var emailTemplate = "Hi %first_name%! Thanks for completing this code challenge :)";

function merge(content, values) { content = content.replace("%first_name%",values.first_name); return content; }

var mergedContent = merge(emailTemplate, mailValues); console.log(mergedContent);

</script>

</body> </html>

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("%first_name%",values.first_name);
  return content;
}

module.exports.merge = merge;

2 Answers

Gabbie Metheny
Gabbie Metheny
33,778 Points

Although this wasn't made explicit in the challenge instructions, I believe %first_name% is just an example of one of many values that your merge function would receive, and it should be able to replace all placeholders in the template with their corresponding values. So you need to loop over the values object, replacing all keys surrounded by '%'s with the corresponding key from the values object, before returning content. Behind the scenes, they're probably testing for some additional values like last_name, age, badge_count, etc, so your code needs to be flexible enough to work for all of those cases. Let me know if you're still struggling with this one!

thanks I tried that as well, but with the content += content.replace and not with the content = content.replace

so know it works thanks

Gabbie Metheny
Gabbie Metheny
33,778 Points

Oh good, glad you figured it out!