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

kushalmahajan2
kushalmahajan2
7,683 Points

Not sure of this implementation going wrong !

I have tried these lines of code below to complete the function merge

content = content.replace( content, "Hi" + values.first_name + "! Thanks for completing this code challenge :)" );

Or

 content =  "Hi " + values.first_name + "! Thanks for completing this code challenge :)";

Or

 content = content.replace("%first_name%", values.first_name );

3 Answers

kushalmahajan2
kushalmahajan2
7,683 Points

Hi Got this working via some digging in some javascript methods. Here

function merge(content, values) {
  // loop over all the keys in the values object
  Object.keys(values).forEach(function(key) {
    // look for the key surrounded by % in the string
    // and replace it by the value from values
    content = content.replace('%' + key + '%', values[key]);
  });
  return content;
}
Dan Henning
Dan Henning
8,691 Points

Hello kushalmahajan2,

It's not clear from your description what you're trying to accomplish. However, it appears that you are merging (concatenating) string values with an object's property value - that is, values.first_name.

Strings are immutable, so the original value cannot be changed. But you can return a modified "copy" of the original string. String.replace() searches the string (in this case content) for the first argument and replaces each instance of it with the second argument.

If you want to concatenate values - including the property value, the second variation is what you want. String.replace() is not necessary. Hope that helps!

var content =  "Hi " + values.first_name + "! Thanks for completing this code challenge :)";

Best regards, Dan

kushalmahajan2
kushalmahajan2
7,683 Points

Hi Dan, I am just trying to complete this code challenge. I didn't pasted the whole files since my query is put up under the same.

The code challenge says " 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"

Let me know if you have access to this code challenge else I can copy paste the code. You can access the files by clicking the breadcrumb link which says "Creating a Basic Template Engine in Node.js"

kushalmahajan2
kushalmahajan2
7,683 Points

This line of code as given by you says your implementation is off. Try Again !

Dan Henning
Dan Henning
8,691 Points

Ahhh! It makes sense now. I'm not used to looking at the bread-crumb links for context. Then String.replace() is required. And your third example should work as expected. If there were multiple values then a for/in loop would be required (see example):

function merge(content, values) {
   for(var key in values) {
      content = content.replace("%" + key + "%", values[key]);
   }
   return content;
}