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

Nathaniel Saludes
Nathaniel Saludes
8,345 Points

The challenge says that my implementation is off but when I tested it locally using node.js, it works anyway.

I did everything it says on the challenge and tested my code locally (which works) but it doesn't seem to accept it.

index.js
var utilities = require("./utilities.js");

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

Steven Parker
Steven Parker
231,275 Points

I can see that your code will handle the sample that only replaces "first_name", but the challenge is asking for a function that would be able to handle larger value sets with different token names as well. I'm sure the validator is testing it with different data from the sample.

Hints: You'll probably need a loop, and don't hard-code any property names.

Nathaniel Saludes
Nathaniel Saludes
8,345 Points

It worked! thanks for the hint... I made a mistake by assuming that it will only test it with the given object property

Thomas Tilton-Heylin
Thomas Tilton-Heylin
12,098 Points
function merge(content, values) {
    for(var key in values) {
        // replace all key with the value from the values object
        content = content.replace("%" + key +"%", values[key]);
    }

  return content;
}


module.exports.merge = merge;

this will complete the challenge. I wish they always had challenge completions up so you can actively double check your work must faster. There is technically no cheating on treehouse.

Steven Parker
Steven Parker
231,275 Points

Well, it may not be "cheating", but they also strongly discourage posting cut-and-paste-able solutions without at least a detailed explanation of how it works. I've seen some cases where such a posting was redacted by staff member or moderator.