
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
Recursive Product of All Digits of a Number in JavaScript
We are required to write a JavaScript function that takes in a number and finds the product of all of its digits. If any digit of the number is 0, then it should be considered and multiplied as 1.
For example − If the number is 5720, then the output should be 70
Example
Following is the code −
const num = 5720; const recursiveProduct = (num, res = 1) => { if(num){ return recursiveProduct(Math.floor(num / 10), res * (num % 10 || 1)); } return res; }; console.log(recursiveProduct(num));
Output
This will produce the following output in console −
70
Advertisements