首页 文章资讯内容详情

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

2026-06-04 1 花语

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

例如:如果输入数字是-

4, 6, 7, 2, 3

那么输出应该是-

7

示例

让我们为该函数编写代码-

// using spread operator to cater any number of elements 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