Maximum call stack size exceeded -> Basic Algorithm Scripting: Factorialize a Number

Maximum call stack size exceeded -> Basic Algorithm Scripting: Factorialize a Number
0

#1

I am working on the Basic Algorithm Scripting: Factorialize a Number exercise. My code is:

function factorialize(num) {
  if (num == 1) {
    return 1;
  }
  else {
    return num * factorialize(num-1);
  }
}

factorialize(5);

however this does not work and gives the error

Maximum call stack size exceeded

I tried my code in another Jac=vaScript IDE and it worked fine and gave the correct answer so is there any reason why this wouldn’t work in FCC?


#2

Your function does not deal with the case of num equal to 0. You create an infinite recursion because the final exit condition of num == 1 is never satisfied, so the else code block executes indefinitely.