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
热门推荐
1 合字的祝福语简短
6 医政科祝福语简短
10 他舅生日祝福语简短
11 公司五一祝福语简短最新
12 婚庆花篮祝福语简短大气
13 中秋妈妈生日祝福语简短
14 万寿祝福语简短
15 简短的祝福语12字
16 叔叔阿姨新年祝福语简短
17 周一情人祝福语简短
18 参加婚礼祝福语大全简短