用js求数组[2,6,1,77,52,25,7]
时间: 2024-05-02 17:18:20 浏览: 16
的最大值和最小值。
```
var arr = [2, 6, 1, 77, 52, 25, 7];
var max = Math.max.apply(null, arr);
var min = Math.min.apply(null, arr);
console.log("最大值:" + max + ",最小值:" + min);
```
输出结果:
```
最大值:77,最小值:1
```
相关问题
用js求数组[2,6,1,77,52,25,7]的最大值
可以使用Math.max()方法来获取数组中的最大值,如下所示:
```js
var arr = [2, 6, 1, 77, 52, 25, 7];
var max = Math.max.apply(null, arr);
console.log(max); // 输出77
```
其中,Math.max()方法可以接收任意数量的参数,并返回这些参数中的最大值。但是它不能直接作用于数组,因此需要使用apply()方法将数组作为参数传递给Math.max()方法。
用js 求数组[2,6,1,77,52,25,7]中的最大值
可以使用JavaScript中的Math.max()方法来求出数组中的最大值。这个方法可以接收任意数量的参数,并返回其中的最大值。
代码如下:
```
let arr = [2, 6, 1, 77, 52, 25, 7];
let max = Math.max(...arr);
console.log(max); // 输出77
```
这里使用了ES6的扩展运算符(...),将数组中的元素打散成单独的参数传递给Math.max()方法。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)