DEV Community

Discussion on: JS FUNCTIONS AND MORE BASIC QUESTIONS

Collapse
 
ignasave profile image
Ignacio Martin Vazquez

Is a recursive function, it says
Function factorial recieves a number.
Then, if the number is less than 2, returns 1, else return the number plus the result of the same function, but passing the number -1. Si if You call factorial of 4, ir would compare if it's less than 2, as isnt, it would return the multiplication of 4 by the result of factorial of 3, then it would compare if 3 is less than 2, it is not so, se have the same thing, it's gonna wait the result of 3 by factorial of 2, 2 is not less than 2, so we get in the implicit end of the recursive call, 2 * factorial of 1 , here, 1 is less than 2, so ir returns 1, and all the return begins to work. If You need more explination search for recursive functions