Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Build a Simple Dynamic Site with Node.js Creating a Basic Template Engine in Node.js A Simple Merge Utility

Roman Smolkin
Roman Smolkin
969 Points

Maximum Call Stack Size Exceeded

It says to look at index.js for how it should work. So content and values are getting passed it, so I just update the content variable using the utilities.merge method right?

var utilities = require("./utilities"); function merge(content, values) { content = utilities.merge(content, values); return content; }

module.exports.merge = merge;

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
var utilities = require("./utilities");
function merge(content, values) {
  content = utilities.merge(content, values);
  return content;
}


module.exports.merge = merge;

1 Answer

Steven Parker
Steven Parker
215,940 Points

When a function calls itself without any kind of filtering condition, it creates a situation known as infinite recursion. This rapidly consumes the available memory on the stack.

Recursion is a useful technique, but is not always the best approach to a particular problem. And when it is, there must be a conditional expression to limit the recursion depth.

In this case, a simple loop would do a good job, similar to the example that was given in the video. You'll also need to add some code to perform the value substitution (also similar to what was done in the video).