[Solved] Javascript – is it possible to make a loop using functions? [closed]


Here’s how you can repeat some code with a recursive timer function:

(function repeat(n){
  if (!n) return;
  console.log("do something");
  setTimeout(repeat, 0, n-1);
})(4);

Contrary to a (non tail optimized) recursion, there’s no cumulative memory consumption here.
This kind of construct is useful in some cases. But just avoiding the for and while keywords doesn’t strike as one of them.

3

solved Javascript – is it possible to make a loop using functions? [closed]