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 教师闺蜜祝福语简短