Factorialize a Number(what if I use num ===1 return 1)

Tell us what’s happening:

Your code so far


function factorialize(num) {
  if(num === 0){//this works but when I try num === 1 it gives an error "call stacks exceeded" ,why?
    return 1;
  }else{
    return num * factorialize(num -1);
  }
}

factorialize(5);

If you use if (num === 1) {return 1}
You get this:

num * factorialize(num - 1)
1 * factorialize(0)
0 is not 1 so it keeps going
1 * 0 * factorialize(-1)
-1 is not 1, so it keeps going
1 * 0 * -1 * factorialize(-2)
etc.

Edit: this happens with factorialize(0), with factorialize(1) it will just return 1

2 Likes