Home  >  Article  >  Web Front-end  >  Sample code for finding the maximum value in an array in javascript_javascript skills

Sample code for finding the maximum value in an array in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:08:301156browse
Copy code The code is as follows:



< ;title>Get the maximum value of the array

<script> <br>//Define the array<br>var arr = [1,4,3,9,5,0, -1,7,22]; <br><br>//The subscript of the maximum value is first assumed to be the subscript of the first element <br>var index = 0; <br>for(var x = 0; x < arr.length; x ){ <br><br>if(arr[index] < arr[x]){ <BR>index = x; <BR>} <BR>} <br><br>document.write("The index is the "arr[index] "maximum" in " index "); <br><br></script>








Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn