Sample code for maximizing arrays in javascript

  • 2020-03-30 00:57:14
  • OfStack

 
<html> 
<head> 
<title> Gets the maximum value of the array </title> 

<script> 
//Define an array
var arr = [1,4,3,9,5,0,-1,7,22]; 

//The index of the maximum value is assumed to be the index of the first element
var index = 0; 
for(var x = 0; x < arr.length; x++){ 

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

document.write(" The index for " + index + " In the " + arr[index] + " The biggest "); 

</script> 

</head> 

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

</html> 

Related articles: