# Solution in JavaScript about the maximum and minimum values in an array and their output in the following table

• 2021-07-01 06:34:40
• OfStack

Today, when learning the array in js, I encountered the maximum and minimum values in the output array and their following table. The following is my own solution!

``````
<script type="text/javascript">
var arr = [14, 14, 53, 14, 14, 53, 67, 67];
var max = arr[0],min = arr[0];
var maxIndex = [];
maxIndex[0] = 0;
var minIndex = [];
minIndex[0] = 0;
var j = 1, k = 1;
for(var i = 1; i < arr.length; i ++){
if(max < arr[i]){
max = arr[i];
maxIndex[0] = i;
}
if(min > arr[i]){
min = arr[i];
minIndex[0] = i;
}
}
for(var i = 0; i < arr.length; i ++){
if(max == arr[i]){
maxIndex[j] = i;
j ++;
}
if(min == arr[i]){
minIndex[k] = i;
k ++;
}
}
document.write(" Maximum values are: " + max + "<br />");
document.write(" The minimum value is: " + min + "<br />");
for(var i = 1; i < minIndex.length; i ++){
document.write(" The subscript of the minimum value is: " + minIndex[i] + "<br />");
}
for(var i = 1; i < maxIndex.length; i ++){
document.write(" The maximum value is subscript to: " + maxIndex[i] + "<br />");
}
</script>``````

Let's introduce how to quickly get the maximum and minimum values in the array in js

``````
var a=[1,2,3,5];
``````