There's a very subtle point at play here. In the following two examples, the body of the function is the same as your original. In one case, an anonymous function is used, while in the other, the function is named "hanoi" just as in the original.
var hanoi2 = function hanoi(disc, src, aux, dst){
if (disc > 0){
hanoi(disc - 1, src, dst, aux);
document.writeln('Move disc ' + disc + ' from ' + src + ' to ' + dst);
hanoi(disc - 1, aux, src, dst);
}
};
var hanoi3 = function(disc, src, aux, dst){
if (disc > 0){
hanoi(disc - 1, src, dst, aux);
document.writeln('Move disc ' + disc + ' from ' + src + ' to ' + dst);
hanoi(disc - 1, aux, src, dst);
}
};
Will either of these work? Note that the body of the function is still referring to "hanoi", not "hanoi2" or "hanoi3".
It turns out that hanoi2 will work, but hanoi3 will not. This is because when you name a function, the name of the function is available inside the scope of the function body. So, in both your original example and in hanoi2, the "hanoi" inside the function isn't referring to the global variable hanoi but rather the named function hanoi.
So, the answer to your question is that because the function uses recursion, the named version is slightly more robust than the anonymous version under modifications to your code.