0

I followed the path of the whole execution and according my calculations it should be return 24, but in fact it returns 7

function factorial(x) {
    if (x < 0) return;
    if (x === 0) return 1;
    return x + 1 * factorial(x - 1);
  }

let x = factorial(3);

console.log(x); //7

I´m likely not understand properly the recursive functions

10
  • 1
    Shouldn't 3! return 6? Commented Apr 11, 2020 at 20:40
  • 2
    I think you mean (x + 1) * factorial(x - 1). Multiplication has a higher order of precedence so in your case the expression would work as if x + (1 * factorial(x - 1)) Commented Apr 11, 2020 at 20:41
  • 1
    Wait, now that I think about it - why do you actually do x + 1? The definition of a factorial is just n * (n - 1) * ... * (n - (n -1)) - you shouldn't be adding anything. Commented Apr 11, 2020 at 20:42
  • has anyone actually ran the function? It returns 2 then twice NaN Commented Apr 11, 2020 at 20:47
  • 1
    @ZombieChowder not sure what you mean. This one returns 7. Commented Apr 11, 2020 at 20:54

1 Answer 1

3

You seem to understand the basics of recursive programming quite well, however I'm not sure you fully understand how the factorial function works.

For example:

3! = 3*2*1 = 6

4! = 4*3*2*1 = 24

You could express this in a recursive way :

a(0) = 1

a(n) = a(n - 1) * n

Implementing this logic in your code you would get the following :

function factorial(x) {
  if (x < 0) return;
  if (x === 0) return 1;
  return x * factorial(x - 1);
}

console.log(factorial(4))

Which returns 24 as expected.

Sign up to request clarification or add additional context in comments.

3 Comments

@StephenDuffy there is an explanation for that, actually. n! can be represented as ((n+1)!) / (n+1), so 3! would be 4! / 4. It makes sense 4! = 4 * 3 * 2 * 1 and if you divide by 4 you're left with 3 * 2 * 1 which is 3!. It's also a mutually recursive factorial definition which is fun. At any rate, if you look at 0! that would be 1! / 1 which gives you 1. There you go. Another way to explain it is permutations - n! is the number of ways to arrange n items. And with zero items there is exactly one arrangement - an empty one.
@StephenDuffy but probably the most accurate explanation for 0! = 1 is also very disappointing. It's as follows: because mathematicians defined it that way. As I said, disappointing. But mathematicians do tend to define things that don't make complete sense in other terms because it then helps out with solving some formulas. Apparently, the number 1 is not prime, because if it was, you'd make some equation that uses primes a lot harder. Hence why 1 is excluded.
@VLAZ: yes, because it's defined that way. But its not arbitrary. As you pointed out, they're defined that way because they are much more useful definitions, both the definition of 0! and the definition of "prime number". More difficult ones are things like 0 ^ 0, which has several competing possibilities; yes there is a best one (0 ^ 0 = 1), and so it's often the accepted one, but there is another reasonable option (0 ^ 0 = 0), so it's much more ambiguous.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.