javascript-algorithms icon indicating copy to clipboard operation
javascript-algorithms copied to clipboard

Add factorialRecursiveTCO (Tail Call Optimized)

Open trainer2001 opened this issue 3 years ago • 1 comments

trainer2001 avatar Dec 12 '21 10:12 trainer2001

This is interesting, I wonder how you test this, since its dependent on the JS engine?

E.g. Chrome doesn't seem to support this: https://chromestatus.com/feature/5516876633341952

Debugger with stack trace:

image

With the code:

function factorialRecursiveTCO(number) {
  function fact(number, accumulator = 1) {
    console.log("fact", number);
    debugger;
    if (number < 2)
      return accumulator;
    else
      return fact(number - 1, accumulator * number);
  }
  console.log("factorialRecursiveTCO", number);
  return fact(number);
}
factorialRecursiveTCO(10)

kungfooman avatar Dec 12 '21 10:12 kungfooman