Factorialize a Number(return)

What a difference between return 1 and return factorial=1?
In the second case i tried to put return 1, but it fails function.


function factorialize(num) {
if (num === 0){
  return 1
}
return factorialize(num-1, factorial*num)
}
factorialize(5);

\\\\\\\\\\\\

function factorialize(num, factorial = 1) {
  if (num == 0) {
    return factorial;
  } else {
    return factorialize(num - 1, factorial * num);
  }
}
factorialize(5);

Challenge: Factorialize a Number
Link to the challenge:

It looks like you are using a different version of recursion that was taught by freeCodeCamp. The default argument version of recursion works fine, but it doesn’t work the same as recursion based on return values (and it sort of dodges the misunderstandings that people usually have with scope, function calls, and return values).

In this case, the function parameter factorial is storing the ultimate return value rather than building the final result from the base case. There might be an explanation of the original author’s intent from wherever you found this code.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.