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.
Therefore, let’s write the code for this function −
Example
The code for this will be −
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
The output in the console will be −
70