-4

Learning about function in javascript and i don't understand how the function is returning 720 as an answer to the operation performed in it.

function factorial(num) {
    if(num <= 0) {
        return 1;
    }else {
        return (num * factorial(num-1) )
    }
}
console.log(factorial(6)) // logs 720. why? 

Here is the link to the tutorial on tutorialspoint, under recurring function I've tried working it on the firefox console, but still can't figure why it 720.

this is what i think should be logged

function factorial(num) {
    if(num <= 0) {
        return 1; // won't return, because 6 is greater and not equal to 0 
    }else {
        return (num * factorial(num-1) ) // bracket first(6-1), 
           //then multiply by 6 (6 * 5 = 30)
    }
}
console.log(factorial(6)) // 30

please help, obviously I'm the one making the error!

Forgive me if this is a rookie way of asking a question on this site, i had to create this account in the hope of getting an answer, thanks.

Romeo
  • 406
  • 3
  • 9
  • 2
    `bracket first(6-1)` - this actually calls factorial function again – Iłya Bursov Apr 10 '18 at 21:54
  • 1
    Well yes, `return num * (num-1)` would have returned `30`. But there's a function call in there! It's actually `return 6 * factorial(5)`. – Bergi Apr 10 '18 at 21:54
  • 4
    You should learn what factorial means before trying to code it. :-) You may also want to do some research into understanding recursion. – Ken White Apr 10 '18 at 21:56
  • 1
    The first time through it is basically `6*5!`, but the next time through, `5!` calculates as `5*4!`, then `4!` calculates as `4*3!` and so on. If you follow it all the way back it would end up with `6*5*4*3*2*1` (ignoring the 0 stuff). – nurdyguy Apr 10 '18 at 21:58
  • You might find this video useful: https://www.youtube.com/watch?v=Mv9NEXX1VHc – DecayConstant Apr 10 '18 at 22:07
  • Possible duplicate of [Calling a javascript function recursively](https://stackoverflow.com/questions/7065120/calling-a-javascript-function-recursively) –  Apr 10 '18 at 22:18
  • 2
    I'm voting to close this question as off-topic because this is a math problem, not a javascript problem. – Randal Schwartz Apr 10 '18 at 22:20

2 Answers2

2

If you actually read the description taken from the page you linked:

Recursion is a technique for iterating over an operation by having a function call itself repeatedly until it arrives at a result. Recursion is best applied when you need to call the same function repeatedly with different parameters from within a loop.

That says it all. "having a function call itself repeatedly". This line return (num * factorial(num-1) ) calls the function again (repeatedly);

gforce301
  • 2,708
  • 1
  • 16
  • 24
0

console.log(fractional(6));

This will call fractional function and num will be 6.

function factorial(num) { // num will be 6
   if(num <= 0) { // 6 greater than 0, hence false and goes to else block
       return 1;
   }else {
       return (num * factorial(num-1) ) //return 6 * factorial(5); Here again factorial function will be executed with value 5 and so on.
   }
}

These are called recursive functions.Check here for more detail and explanation.

Sailesh Babu Doppalapudi
  • 1,459
  • 1
  • 10
  • 20