Factorial Recursive with JavaScript

Introduction

Recursive is relating to or involving the repeated application of a rule, definition, or procedure to successive results.

Definitions

Recursion (adjective: recursive) occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathematics and computer science, where a function being defined is applied within its own definition. While this apparently defines an infinite number of instances (function values), it is often done in such a way that no infinite loop or infinite chain of references can occur.

Learn more: https://en.wikipedia.org/wiki/Recursion

In mathematics, The factorial function to multiply all whole numbers from our chosen number down to 1

5! = 5 * 4 * 3 * 2 * 1 = 120

Thus, Factorial Function is a good example to use Recursive Function.

recursive function is a function that calls itself during its execution. This enables the function to repeat itself several times, outputting the result and the end of each iteration.

function recursive_factorial(n) {
  if (n <= 1) {
    return 1;
  }
  return n * recursive_factorial(n - 1);
}
console.log(recursive_factorial(11));
3628800

Above result is

11 * 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 = 3628800

Leave a Reply

Your email address will not be published.

ANOTE.DEV