It works, but I have a question about the code


#1

I used this code and it works fine:

//Remember to set your condition outside the loop!
var count = 0;

var loop = function(loop){
while(count<3){
console.log("I'm looping!");
count++;
}
};

loop();

My questions is about the var loop = function(loop). It also works as: var loop = function(). So is it necessary to write function (loop)?


#2

Well the idea is that the things in the () after functions declare local variables and that those variables get their values by the function call e.g.

var square = function(x){
    return x*x;
}

square(2);

here x is the local variable and by using square 2 you run the function with x=2, this is neat because you can change the value of x with with every function call square(3) would e.g. yield 9 although you're running the exact same function.

So the reason this is working for you is because your parameter is actually redundant. You don't use it anywhere in the function and you don't pass a value to it ( loop() -> no value -> undefined ). So you're actually better of using function() if you don't use it. Also using the function name inside of the function as local variable is not a good idea, because you may get in the situation where you overwrite your function inside itself which would make it useless or in case you use a local variable (like in your case) you might might shadow the function meaning you're no longer able to call the function inside itself. This might not be that important by now but still I'd consider this a bad habit.

Hope this makes it a little clearer.


#3

Thank you, that clears up things quite a bit.