程序查找三个数字中的最大值 - JavaScript

javascriptweb developmentfront end technologyobject oriented programming更新于 2025/12/4 23:40:17

我们需要编写一个 JavaScript 函数,该函数接受三个或更多数字并返回这些数字中的最大值。

例如:如果输入的数字是 −

4, 6, 7, 2, 3

那么输出应该是 −

7

示例

让我们来编写该函数的代码 −

// 使用扩展运算符来满足任意数量的元素
const findGreatest = (...nums) => {
   let max = -Infinity;
   for(let i = 0; i < nums.length; i++){
      if(nums[i] > max){
         max = nums[i];
      };
   };
   return max;
};
console.log(findGreatest(5, 6, 3, 5, 7, 5));

输出

控制台中的输出 −

7

相关文章


有用资源