JavaScript获取数组最大值和最小值

时间:2020-11-26 15:15:12

1、实现源码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JavaScript获取数组最大值和最小值</title>
<script type="text/javascript">
//获取数组最大值
function findMax()
{
var arr = [3,4,76,2,54,23,78,90,89,99,234,123,456,23,76,89,90];
var max = Array.max(arr);
document.getElementById("max").value = max;
}

//获取数组最大值函数
Array.max = function(arr)
{
return Math.max.apply(Math,arr);
}

//获取数组最小值
function findMin()
{
var arr = [34,4,3,67,89,30,23];
var min = Array.min(arr);
document.getElementById("min").value = min;
}

//获取数组最小值函数
Array.min = function(arr)
{
return Math.min.apply(Math,arr);
}

window.onload = function(){
findMax();
findMin();
};
</script>
</head>

<body>
<label>数组最大值:</label>
<input type="text" id="max"/>
<label>数组最小值:</label>
<input type="text" id="min"/>
</body>
</html>

2、实现结果

JavaScript获取数组最大值和最小值