user5579973
user5579973

Reputation:

Find the Max Value in an array using JavaScript function

I have this function, the purpose is to find the maximum (number) element in an array, the value is coming from an argument. Can you please explain what is going on line by line with the code below.

function max(arr){
 var max = arr[0];
 for(var i=1; i<arr.length; i++){
   if(arr[i] > max){
     max = arr[i];   
   }
  }
return max;
}

max([4,12,3,8,0,22,56]); //output is 56

Upvotes: 4

Views: 18154

Answers (6)

danklad
danklad

Reputation: 118

Get max and min number of an array in javascript manually..

function max(array){
    var max= array[0];
    var min = array[0];
    for(var i=1;i<arr.length;i++){
        if(array[i]>max){
            max= array[i];
        }
        
        if(array[i]<min){
            min=array[i];
        }
        
    }
    console.log(min+","+max);
    }

Upvotes: 0

Sasi Kumar M
Sasi Kumar M

Reputation: 2630

In my case, had to find all the maximum values,

var max = -Infinity, result = [];
 for (var i = 0; i < arr.length; ++i) {
    if (arr[i] < max) continue;
    if (arr[i] > max) {
      result = [];
      max = arr[i];
    }
   result.push(max);
}
return result; // in case of number of occurrences of max values return result.length;

Upvotes: 0

Another way to use reduce: .reduce((a,c) => c > a ? c : a, 0)

Upvotes: 0

Adi
Adi

Reputation: 119

Below are three steps of your code.

  1. Your code starts to compare first element in array with next element.
  2. In if condition it check for maximum and stores in maximum (max) if the condition true.
  3. And finally returns max value.

So my suggestion is to use JavaScript inbuilt reduce method.Below are the reasons.

Math.max.apply or apply method will either fail or return the wrong result if the array has too many elements, because they try to pass the array elements as function parameters.

So use reduce method which don't have this problem

var arr = [2,4,6];
var max = arr.reduce(function(a, b) {
    return Math.max(a, b);
});
console.log(max);

Upvotes: 1

Leo Farmer
Leo Farmer

Reputation: 7910

Line one. max function is passed the array [4,12,3,8,0,22,56],

Line two. max variable is set to the first item in the array. max is 4,

Line three. A for loop is started which will start from 1 and keep looping for the length of the arr array.

Line four. Checks if the current array value is larger than the max value.

So, first time around it checks if arr[1] is greater then max. If yes, max is set to arr[1] First loop checks if (12 > 4) it is so max = 12

Second time around it checks if arr[2] is greater than max. Second loop checks if(3 > 12) it is not so max = 12

Third time around it checks if arr[3] is greater then max. Third loop checks if (8 > 12) it is not so max = 12

Fourth time around it checks if arr[4] is greater then max. Fourth loop checks if (0 > 12) it is not so max = 12

Fifth time around it checks if arr[5] is greater then max. Fifth loop checks if (22 > 12) it is so max = 22

Sixth time around it checks if arr[6] is greater then max. Sixth loop checks if (56 > 22) it is so max = 56

Line eight. The loop has finished and max is returned. max is 56

Upvotes: 2

RomanPerekhrest
RomanPerekhrest

Reputation: 92904

Before going through the loop, your function accepts the first array element's value as a "starting"(initial) maximum. That value was 4.
On every loop iteration, each array value is compared with initial "maximum".
If the current value is bigger than previous maximum value - that current value overrides it and becomes the maximum.
But ... there is much easier and better way: is to use built-in Javascript objects, like Math.
Consider the following search of max value:

var arr = [4,12,3,80,0,22,56];
var max = Math.max.apply(null, arr);
console.log(max); // 80

The above approach is preferable for searching min/max values.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max

Upvotes: 4

Related Questions