查找数字中最大和最小数字之间的差异-JavaScript
我们需要编写一个JavaScript函数,该函数接受一个数字并返回其中存在的最大和最小数字之间的差。
例如:如果数字为5464676,则此处的最小数字为4,最大数字为7
因此,我们的输出应为3
示例
让我们为该函数编写代码-
const num = 44353456;
const difference = (num, min = Infinity, max = -Infinity) => {
if(num){
const digit = num % 10;
return difference(Math.floor(num / 10), Math.min(digit, min),
Math.max(digit, max));
};
return max - min;
};
console.log(difference(num));输出结果
控制台中的输出:-
3