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
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短