通过递归查找最大位数-JavaScript
我们需要编写一个JavaScript递归函数,该函数需要一个数字并返回数字中的最大数字。
例如:如果数字是-
45654356
那么返回值应该是6
示例
以下是代码-
const num = 45654356;
const greatestDigit = (num = 0, greatest = 0) => {
if(num){
const max = Math.max(num % 10, greatest);
return greatestDigit(Math.floor(num / 10), max);
};
return greatest;
};
console.log(greatestDigit(num));输出结果
以下是控制台中的输出-
6
热门推荐
10 办喜事祝福语简短致辞
11 体育老师简短祝福语
12 六六妈妈生日祝福语简短
13 立冬来临怎么祝福语简短
14 鲜花卡片简短的祝福语
15 爱情生日祝福语 简短独特
16 少女节祝福语简短优美
17 队长生日祝福语简短
18 金哥哥结婚祝福语简短