查找数字中最大数字和最小数字之间的差异 - JavaScript

javascriptweb developmentfront end technologyobject oriented programming更新于 2025/12/4 19:56:17

我们需要编写一个 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

相关文章


有用资源