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 JavaScript Foundations Functions Anonymous Functions

Brian CI
Brian CI
28,971 Points

My function is only being called once

var myFunction = function () {
    console.log('myFunction was called');
};

var callTwice = function (targetFunction) {
    targetFunction();
    targetFunction();
};

callTwice(myFunction);

It should be called twice but I only get the output "myFunction was called" once.

Also why does Jim use }; sometimes but other times just }? See screen shot: http://s28.postimg.org/xnvdoti59/image.png

4 Answers

Check your output thoroughly. Firefox won't display the output text twice, instead it'll show a counter on the right side of the console to display how many times the same message has been logged.

Brian CI
Brian CI
28,971 Points

I have seen that before and this was part of it.

I also noticed that in the tutorial he leaves in a line "undeclaredVariable;" which I didn't put above but I think I had in my code. I don't know if this works in chrome but in firefox it stops executing at that line. So I think I had two issues. I'm coming back and looking at this after a couple weeks.

it's line 53 in the video and you can see it around 9.40. You cannot continue with the tutorial in firefox without either removing or commenting out that line.

What I thought was being displayed only once was actually the original console.log('myFunction was called'); on line 52 and it was only appearing once.

I don't know if this makes sense but in short, in firefox, the "Hello from the anon function!" or 'myFunction was called' will only be displayed twice if there are no lines "undeclaredVariable;"

Brian CI
Brian CI
28,971 Points

It also seems like there's a ';' missing at the end of line 72 at the end of the video. It doesn't work for me without adding it.

Hi Brian,

I am copying and pasting your code in my console, and the function gets called twice... I think Jim just forgot the semi-colon there.

Brian CI
Brian CI
28,971 Points

I just get it once... can this be some kind of peculiarity of FireFox vs. Chrome?

hm. I am using Chrome. Are you sure you just don't see it? Can you stretch the console to make sure you're not missing anything?