How to write the factorial function with reduce and range in JavaScript?


In this problem statement, our aim is to write the factorial function with reduce and range with the help of Javascript. So basically range and reduce are the predefined functions of Javascript.

What is the use of reduce and range functions in Javascript ?

In Javascript the reduce and range functions are the most useful function when working with arrays.

The reduce function takes an array and reduces it to a single value by processing a function for every item of the array. The function takes two parameters, the first one is the accumulator which stores the result of the previous solution and the second is the current value of the array. So the result of every calculation is stored in the accumulator and passed on to the next iteration.

For example

const nums = [1, 2, 3, 4, 5];
const sum = nums.reduce((acc, val) => acc + val, 0);
console.log(sum);

And the range function in Javascript is used to generate an array of numbers from a starting value to an ending value with a given step size. For example create an array of numbers from 1 to 10 −

const range = (start, end, step) => Array.from({ length: Math.floor((end - start) / step) + 1 }, (_, i) => start + (i * step));
const numbers = range(1, 10, 1);
console.log(numbers);

Understanding the problem statement

The problem statement says to write a function in Javascript which can calculate the factorial of a given input number with the help of reduce and range functions. So we have to create a method which takes a number as a parameter and returns the factorial of that number. We need to write a code for the function and provide an example usage and analyze the time complexity of the implementation.

Factorial of a non-negative integer n is the product of all positive integers less than or equal to n. For example the factorial of 4 is 4 * 3 * 2 * 1 = 24

Algorithm

Step 1 − Define a function to calculate the factorial of number num.

Step 2 − Inside the function we will use the range function of array to give the range for the factorial.

Step 3 − Now use the reduce method to calculate and process each value to find the factorial at the end.

Step 4 − Show the output as the factorial of the number.

Code for the algorithm

// function for finding the factorial of n
function factorial(num) {
   return Array.from({length: num}, (_, i) => i + 1)
   .reduce((acc, val) => acc * val, 1);
}
console.log(factorial(5));
console.log(factorial(10));
console.log(factorial(0));  

Complexity

The code's time complexity is O(n) where n is the size of the array required to find the factorial of the number. And we have also used reduce function to loop through the numbers. The algorithm's space complexity is O(1) which is constant because we are simply storing the factorial of the provided numbers.

Conclusion

The above code provides a simple and efficient solution to find the factorial with the usage of reduce and range functions in Javascript. So it has an O(n) time complexity and O(1) space complexity.

Updated on: 18-May-2023

603 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements