
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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.
- Related Articles
- How to write recursive Python Function to find factorial?
- Sorting Array with JavaScript reduce function - JavaScript
- Comparing the performance of recursive and looped factorial function in JavaScript
- Function to compute factorial of a number in JavaScript
- How to make a function that returns the factorial of each integer in an array JavaScript
- Average with the Reduce Method in JavaScript
- How to reduce arrays in JavaScript?
- Counting prime numbers that reduce to 1 within a range using JavaScript
- Encoding decimal to factorial and back in JavaScript
- How can I write a MySQL stored function that calculates the factorial of a given number?
- Solution to the clumsy factorial problem in JavaScript
- Count factorial numbers in a given range in C++
- How to call jQuery function with JavaScript function?
- How to create custom range sliders with CSS and JavaScript?\n\n
- How to apply the reduce method for objects in JavaScript?
