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

anmo20
anmo20
6,470 Points

Why isn't my solution working?

It wants me to merge the two values, but it's not accepting my solution. I confirmed this is spitting out the results it is looking for by running this in the browser myself, since this exercise isn't allowing me to preview anything I'm doing.

The function I wrote to test my solution is this:

function merge(content, values){
   console.log(content);            //returns "Hi %first_name%! Thanks for completing this code challenge :)"
   console.log(values.first_name);   //returns "Janet"
   content = content.replace("%first_name%", values.first_name);   
   console.log(content);    //returns "Hi Janet! Thanks for completing this code challenge :)"
   return content;
}

So of course, I put it in without all the console.log, like this

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

Despite the returned string being identical to what the index.js comment is asking for...it's not accepting it. What's up??

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;

1 Answer

anmo20
anmo20
6,470 Points

Apparently it wants you to iterate over it...so I did this as a solution at it worked.

(the console log was just there for me to test it)

function merge(content, values){
   for (let key in values) {
      content = content.replace(`%${key}%`, values[key]);
      console.log(content);
   }
   return content;
}