# JavaScript Find the Max Value in an array using JavaScript function find value array javascript,find value array php,find value

I have this function, the purpose is to find the maximum (number) element in an array, the value is coming from an argument. I'm having a hard time understanding the logic, well I'm old and slow so pardon me. 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
``````

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

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

## CSS how is width getting set?

I have inherited a web page that has a slide show on it and I can't figure out how to change the width of the <article> containing the slide show. I think it is getting size via JavaScript, but ...

I have inherited a web page that has a slide show on it and I can't figure out how to change the width of the <article> containing the slide show. I think it is getting size via JavaScript, but ...

1. getting width of element javascript
2. getting width in golf swing
3. getting width of element jquery
4. getting width of div
5. getting width in your golf swing
6. getting width of an element
7. getting width and height
8. svg getting width

## Ng-transclude not working with table element

I am trying to build a directive for a table to display data. The table has a header and a body. Ideally I would like to have the following directives: <div my-table></div> <div my-...

I am trying to build a directive for a table to display data. The table has a header and a body. Ideally I would like to have the following directives: <div my-table></div> <div my-...

## How to copy multiple radio list buttons into a textarea and change skeleton into sound

I have no idea where to start with this and came here to learn how to do it. I have the following radio lists and textarea(#mcsound): <input type="radio" name="skeleton" id="1mcskel1" value="Top" ...

I have no idea where to start with this and came here to learn how to do it. I have the following radio lists and textarea(#mcsound): <input type="radio" name="skeleton" id="1mcskel1" value="Top" ...

## Sorting an array that is the result of a \$resource call in AngularJS

I'm trying to get the results of an API call and save it to \$scope.segments, so that later I can sort the array using \$scope.segments.sort() However, since \$scope.segments = SegmentsService....

I'm trying to get the results of an API call and save it to \$scope.segments, so that later I can sort the array using \$scope.segments.sort() However, since \$scope.segments = SegmentsService....

1