|
数组如何求最大值,想必很多的朋友都不会吧,本文为大家介绍下javascript中数组是如何求最大值的,感兴趣的朋友不要错过
复制代码 代码如下: <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>
|
|