通过递归查找最大位数-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
热门推荐
3 带清的简短祝福语
10 元旦的祝福语简短 公司
11 生日果酒祝福语简短独特
12 新年给妹妹祝福语简短
13 英文祝福语简短带名字
14 高考前祝福语简短字句
15 结婚祝福语简短精辟好记
16 祝老婆生日祝福语简短
17 群里祝福语简短幽默
18 祖国生日祝福语简短英文