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 trialEldy Voon
3,812 PointsOn about line 18, make the anonymous function with the code 'window.didExecute = true' execute.
var anonymousFunction = function(){};
anonymousFunction = (function () {
window.didExecute = true;
});
8 Answers
Fred Bucci
7,709 PointsFor anyone who is still confused, all you need to do is:
var anonymousFunction = function() {};
(function () {
window.didExecute = true;
}) ();
http://helephant.com/2008/08/23/javascript-anonymous-functions/
omg guys we've been breaking our heads for nothing loll All it asks is to make the self executing function execute which all it was missing were the parentheses, it never said to make the self executing function execute the anonymousFunction variable! hahah Seems a bit of a mixup of explanation and variable confusion. I was stuck on this for a good hour or so! XD Hope this helps anyone confused about it.
Carlos Eduardo Bruno Pereira
Courses Plus Student 6,928 Pointsjust put the open and close parenthesis at the end of the function
(anonymousFunction = (function () { window.didExecute = true; })();
Eldy Voon
3,812 Pointsnope
Joannie Johnson
7,997 PointsThanks lots - seeing the code along with screenshot above really helped.
Kelvin Yu
2,554 PointsIn the video prior to the this task, Jim placed 2 random arguments in the empty parenthesis to immediately pass the anonymous function like so:
(function () {
var a, b, c;
// ...
console.log('from anon function')
})(arg1, arg2)
My question is, why would this not work in the task? Also, what is the difference when using empty vs. filled parenthesis in an anonymous function?
Kevin Murphy
24,380 PointsKelvin, I struggled for awhile with this challenge question and after figuring it out, had (have) the same questions as you. I think further elaboration/clarification is required in the video when that topic is introduced. Particularly what role the arguments (or lack of arguments) play in an anon function when it's used as an object (as per your screenshot).
Kevin Murphy
24,380 PointsKelvin, I share same questions as you. Struggled for awhile with challenge question. I believe the role of args in an anon function used as an object could benefit from further elaboration in the video.
Michael Thomas
10,910 PointsI second that, I passed in (1, 'hello') like Jim did at the end of video, but instead the answer is to just put () - I'm none the wiser as to why that works.
Jim Withington
12,025 PointsI wondered the same thing: if empty parentheses works, why does passing it some sort of value not work?
davinder rehal
888 Pointsvar anonymousFunction = function() {};
(function () {
window.didExecute = true;
})();
Carlos Eduardo Bruno Pereira
Courses Plus Student 6,928 Pointsdon't forget the first open parenthesis before "anonymousFunction". I saw that you didn't put it there.
Eldy Voon
3,812 PointsI don't get it
Carlos Eduardo Bruno Pereira
Courses Plus Student 6,928 Pointssorry, I've wrong. Actually, you don't need to put the 'anonymousFunction' before the function itself. On line 18, the function is already there, just put the open and close parenthesis at the end of it. Like this: (function () { window.didExecute = true; })(); see? No 'anomFunction' before it. Try it, and if it doesn't work, sorry but I don't know what else it can be.
Josh Flowers
7,010 PointsEldy Voon, Carlos is correct. All you need to do is add a set of empty parenthesis as there are no arguments to pass into it.
Elias Thanasopoulos
32,286 PointsWhat it worked for me in the 2nd task is the below:
(function () {
window.didExecute=true;
})()(anonymousFunction);
ilyass mougar
Courses Plus Student 2,858 PointsThis is worked (function () {
window.didExecute=true;
})()();
Joannie Johnson
7,997 PointsJoannie Johnson
7,997 PointsThank you for saving my sanity! I really appreciate seeing this screenshot.
Kelvin Knighton
6,168 PointsKelvin Knighton
6,168 Pointsthanks man, i was starting to hyperventilate.
tuukka uosukainen
22,107 Pointstuukka uosukainen
22,107 PointsThank you for this. I got confused since the task says βon about line 18β.
Jon Orri Leosson
9,506 PointsJon Orri Leosson
9,506 PointsThis is weird but it works. Thank you!
Miriam Shapiro
Courses Plus Student 1,887 PointsMiriam Shapiro
Courses Plus Student 1,887 PointsIncredibly helpful! I was stuck for so long!
Michael Paccione
9,017 PointsMichael Paccione
9,017 PointsDue to your help my computer was spared from a 30 foot drop to the silicon splitting concrete below. My box thanks you!
Elisabeth Meyer
6,231 PointsElisabeth Meyer
6,231 PointsYou have my sincere gratitude, Fred. I was nearly two cups of tea into this problem before I saw your comment. Hah. Thank you.
Jamil Jones
10,852 PointsJamil Jones
10,852 PointsSure did!! Thanks my Friend!