- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Recursive multiplication in array - JavaScript
We are required to write a JavaScript function that takes in an array of nested arrays of Numbers and some false values (including 0) and some strings as wel. the function should return the product of number values present in the nested array.
If the array contains some 0s, we should ignore them as well. Let’s write the code for this function −
Example
const arr = [1, 5, 2, null, [ 2, 5, null, undefined, false, 5, [ 1, 3, false, 0, 2 ], 4, 2, false ], 4, 6, 0 ]; const recursiveMultiplication = arr => { let prod = 1; for(let i = 0; i < arr.length; i++){ if(Array.isArray(arr[i])){ prod *= recursiveMultiplication(arr[i]); }else{ prod *= arr[i] || 1; }; }; return prod; }; console.log(recursiveMultiplication(arr));
Output
Following is the output in the console −
576000
- Related Articles
- Alternate addition multiplication in an array - JavaScript
- Addition multiplication ladder in an array in JavaScript
- Using merge sort to recursive sort an array JavaScript
- JavaScript recursive loop to sum all integers from nested array?
- JavaScript Quicksort recursive
- Recursive Staircase problem in JavaScript
- Using a recursive function to capitalize each word in an array in JavaScript
- What is Multiplication Operator (*) in JavaScript?
- Algorithm for matrix multiplication in JavaScript
- Count divisors of array multiplication in C++
- JavaScript code for recursive Fibonacci series
- Recursive product of summed digits JavaScript
- Recursive string parsing into object - JavaScript
- What is Multiplication Assignment Operator (*=) in JavaScript?
- Return correct value from recursive indexOf in JavaScript?

Advertisements