
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding Product of an Array Using Recursion in JavaScript
We are required to write a JavaScript function that takes in an array of Integers. Our function should do the following two things −
Make use of a recursive approach.
Calculate the product of all the elements in the array.
And finally, it should return the product.
For example −
If the input array is −
const arr = [1, 3, 6, .2, 2, 5];
Then the output should be −
const output = 36;
Example
The code for this will be −
const arr = [1, 3, 6, .2, 2, 5]; const arrayProduct = ([front, ...end]) => { if (front === undefined) { return 1; }; return front * arrayProduct(end); }; console.log(arrayProduct(arr));
Output
And the output in the console will be −
36
Advertisements