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 trialLanre TH
1,037 PointsComplete the implementation of the merge method in utilities.js file. You should be able to pass in a string with placeh
I don't understand, its not working
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 :)";
function merge(content, values) {
for (var key in content){
content = content.replace("%key%",values);
}
return content;
}
module.exports.merge = merge;
8 Answers
Clinton Johnson
28,714 Pointsfunction merge(content, values) { for(var key in values) { content = content.replace('%'+key+'%', values[key]); } return content; } // So to piggy back off of Steven Parker you want to put values[key] because it is an object literal of an array which means that the function merge will loop through the values of the key -> values[key] <- thats what this line of code means. Just remember that you have to specify everything to the t in order for the computer to read your line of code properly and in this case think of it like this if you leave the values empty <- Then your are not specifying in particular where you want the computer to get the values from, hence placing this -> [key] next to value is telling the computer to look in the key and retrieve the values from there. Sorry if i was longwinded hope this helps.
Steven Parker
231,269 PointsYou're getting close, but you have a few issues yet:
- the content is the string you're replacing in — you'll want to loop through the values
- instead of the word "key", you'll need to concatenate the actual key between two percent signs
- you'll want to replace the match string with the specific value associated with that key
Lanre TH
1,037 Pointsfunction merge(content, values) { for (var key in content){ content = content.replace("Hi %"+key+"%! Thanks for completing this code challenge :)",values); } return content; }
module.exports.merge = merge;
Lanre TH
1,037 PointsI am not still getting it, please I need you help Steven Parker
Steven Parker
231,269 PointsWhat does your code look like after you apply the 3 hints I gave you above?
Steven Parker
231,269 PointsIt looks like you only implemented one of the three hints.
Plus you got the concatenation right, but you added some extra words to the replacement string that are not part of the challenge. Lets look at the other two hints again (and a new one) in the context of the code:
for (var key in content)
you'll want to loop through the values (not the content)
"Hi %"+key+"%! Thanks for completing this code challenge :)"
don't add anything to the content
...content.replace("%"+key+"%", values);
replace using the specific value associated with the key
Remember, values is a complex object but you can use the key as an index to get a specific value from it.
If any of these hints are still not clear, I strongly suggest re-watching the video on Binding Values. It walks you through a task very similar to this one.
Khumoyunmirzo Nosirov
8,689 Pointsfunction merge(content, values) { for(var key in values){ content = content.replace("{{"%" + key + "%"}}", values[key]); } return content; }
module.exports.merge = merge;
I read all comments but still I am having same errors , please steve parker help me
catarinabrendel
11,898 PointsKhumoyunmirzo Nosirov you need to remove the curly brackets. In the code provided it uses the '%' symbol to indicate the values to be removed and not the '}' symbol.
content.replace("{{"%" + key + "%"}}", values[key]); <-- specifically in this line
Adem Krasniqi
7,967 PointsThis is what worked for me, taken from another community answer to this challenge:
function merge(content, values) {
for (key in values) {
content = content.replace("%"+key+"%", values[key]);
}
return content;
}
module.exports.merge = merge;