Introduction
A recursive function is a function that calls itself, typically with a different input each time. One common example of a recursive function is a factorial function, which calculates the factorial of a given number.
The factorial of a number is the product of that number and all the positive integers less than it. For example, the factorial of 5 (written as 5!) is 5 x 4 x 3 x 2 x 1, or 120. The factorial of 0 is defined as 1.
In this article, we will discuss how to carry out a recursive function factorial in JavaScript
Using Recursion Function to calculate Factorial of a Number in JavaScript
Example-1
Here's an example of a JavaScript function that uses recursion to calculate the factorial of a given number:
function factorial(n) {
// Base case, if n is 0 or 1, return 1
if (n === 0 || n === 1) {
return 1;
}
// Recursive case, return n multiplied by the factorial of n-1
else {
return n * factorial(n - 1);
}
}
console.log(factorial(5)); // 120
console.log(factorial(10)); // 3628800
In this example, the factorial function takes a single argument, n
, which represents the number for which we want to calculate the factorial. The function uses an if-else statement to check if n
is equal to 0
or 1
. If it is, the function returns 1
. This is the base case of the recursion, where the function stops calling itself.
If n
is not equal to 0
or 1
, the function returns n
multiplied by the factorial of n-1
. This is the recursive case of the function, where it calls itself with n-1
as the argument, and uses the result of that call as part of the final calculation.
This recursion continues until the base case is reached, where the function returns 1
and the recursive calls "unwind" and multiply their results together until the final factorial is returned.
Example-2
Recursive functions can be an elegant and concise way to solve certain problems, especially those that can be expressed in terms of smaller subproblems. In the case of the factorial function, the problem of calculating the factorial of a number can be expressed in terms of calculating the factorial of a smaller number, making it a good candidate for a recursive solution.
Recursive functions can be used to calculate the factorial of a number because the factorial of a number can be expressed in terms of the factorial of a smaller number. For example, the factorial of 5 can be expressed as 5 x the factorial of 4, which in turn can be expressed as 4 x the factorial of 3, and so on. This pattern allows us to define a recursive function that calculates the factorial of a number by calling itself with a smaller input until it reaches the base case of 0, at which point it returns 1.
The typical way to achieve a factorial function might be to use the for loop approach can be seen below
function factorial(n) {
let value = 1;
for (let index = 1; index <= n; index++) {
value *= index;
}
return value;
}
console.log(factorial(5));
Output
120
However, here is an example of a recursive function that calculates the factorial of a given number in JavaScript
function factorial(n) {
if (n === 0) {
return 1;
}
return n * factorial(n - 1);
}
console.log(factorial(5));
Output
120
This function works by first checking if the input n
is 0. If it is, it returns 1, which is the base case. If n
is not 0, it returns n
multiplied by the result of calling itself with n - 1
. This process continues until the function reaches the base case of 0, at which point it starts returning the results of the previous calls, working its way back up the recursive chain.
Example-3
Another method to calculate the factorial of a number is to use the Array.prototype.reduce()
method which is used to apply a function to each element in the array, resulting in a single output value.
function factorial(n) {
return Array.from({length: n}, (_, i) => i + 1).reduce((acc, cur) => acc * cur);
}
console.log(factorial(5)); // 120
console.log(factorial(10)); // 3628800
In this example, the factorial function takes a single argument, n
, which represents the number for which we want to calculate the factorial. The function first uses the Array.from()
method to create an array of numbers from 1
to n
. Then it uses the Array.prototype.reduce()
method to iterate over the array, where the first argument of the reduce function is the accumulator and the second argument is the current value.
Summary
In summary, a recursive function is a function that calls itself, typically with a different input each time. A factorial function is a common example of a recursive function, which calculates the factorial of a given number by expressing the factorial of a larger number in terms of the factorial of a smaller number. While recursive functions can be an elegant solution for certain problems, they can also be slower and less efficient than iterative solutions, especially for large inputs.
However, it is important to note that recursive functions can also be slower and less efficient than iterative solutions, especially for large inputs. This is because each recursive call adds a new level to the call stack, which consumes memory and resources. In some cases, it may be more efficient to use an iterative solution, such as a loop, to calculate the factorial of a number.
References
Recursion - MDN Web Docs Glossary: Definitions of Web-related terms | MDN (mozilla.org)