当前位置:Gxlcms > JavaScript > javascript中数组中求最大值示例代码

javascript中数组中求最大值示例代码

时间:2021-07-01 10:21:17 帮助过:5人阅读

代码如下:

<html>
<head>
<title>数组的最大值的获取</title>

<script>
//定义数组
var arr = [1,4,3,9,5,0,-1,7,22];

//最大值的下标,先假定为第一个元素的下标
var index = 0;
for(var x = 0; x < arr.length; x++){

if(arr[index] < arr[x]){
index = x;
}
}

document.write("索引为" + index + "中的" + arr[index] + "最大");

</script>

</head>

<body>
<div id="time"></div>
</body>

</html>

人气教程排行