JavaScript: how do I improve this repeat function using tail call optimization

41 Views Asked by At

Here is a repeat function that recursively calls itself

  const repeat = x => f => {
    if (x > 0) {
      f()
      return repeat (x - 1) (f)
    }
  }
  

The problem with it is it can cause stack overflow. I was wondering if I can rewrite it using tail call recursion so it can be stack-safe.

0

There are 0 best solutions below