JavaScript中数字可能更大的数字差异
我们需要编写一个带数字的JavaScript函数。然后,该函数应返回该数字的任意两位之间存在的最大差。
换句话说,该函数应该简单地返回其中存在的最大和最小数字之间的差。
例如:
If the number is 654646, Then the smallest digit here is 4 and the greatest is 6 Hence, our output should be 2
示例
为此的代码将是-
const num = 654646;
const maxDifference = (num, min = Infinity, max = -Infinity) => {
if(num){
const digit = num % 10;
return maxDifference(Math.floor(num / 10), Math.min(digit, min),
Math.max(digit, max));
};
return max - min;
};
console.log(maxDifference(num));输出结果
控制台中的输出-
2
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语