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 同事孩子上学祝福语简短