数字所有位的递归乘积—— JavaScript
我们要求编写一个 JavaScript 函数,它接受一个数字,并求出所有数字的乘积。如果数字的任何一位是 0,那就应该考虑并乘以 1。
例如,如果数字是 5720,则输出应该是 70
示例
以下就是代码
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));输出
这将在控制台中产生以下输出
70
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP