# JS seeks the maximum element method in Number type arrays

How to use JS, find the maximum (or minimum) number in an array of Number types?

Four methods are described below.

1. Do not use any library function

` Function Findmax1 (Arr) {let Result = 0; IF (array.isaray (arr)) {for (let i = 0; i ` result? arr [i]: result;} returna result;} else {console.Error ('the parameter arr is Not an array '); return;}}  ` `
Explanation:
Use a variable RESULT to store the maximum value. Traverse the array to be found, if the current The elements traversed by the traverses are greater than Result.

2. Using Array.Reduce ()

The code is as follows:

Function Findmax2 (Arr) {let result = 0; IF (array.isaray (arr)) {result = arr.reduce ((a, b) => {RETURN A> B? A: B;}, 0); Return Result;} else {Console.Error (‘the parameter arr is not an arraY ‘);}}
` Explanation: ` ` The reduuCe method is through a function, for each of the accumulator and the array An element that eventually reducing an array to a value. `
Reduce Accepts two parameters:

The first is Callback, which is the function mentioned earlier. It has four parameters:

Accumulator: accumulator, it is the result of the last Callback. If INITIALVALUE is provided, the first time is initialvalue;

CurrentValue: The element of the array of traverses;

CurrentIndex: Index of the elements of the array of traverses, starting from 0. If INITIALVALUE is provided, then start from 1;

Array: An array of reduced applications.
1. The second is initialvalue, an initial value, as the first first argument that runs the Callback function. This is optional. Here you should pay attention to it, if this parameter is not available, and it is applied to an empty number, it will report an error.
2. Then, the above code means that each traversal array is compared, and it will stay, the Accumulator, and use it as a comparison of the next and array elements. Finally, leaving only this value, the maximum value.
3. 3. Using Apply and Math.max ()

4. The code is as follows:

Function Findmax3 (Arr ) {let result = 0; IF (Array.isArray (arr)){Result = Math.max.Apply (NULL, ARR); Return Result;} Else {Console.Error (‘The parameter arr is not an array’); return;}}

Explanation:

Apply interpretation is slightly complex, not further explanation.

It accepts two parameters, the first is Thisarg, the second is ArgsArray. Both are optional. This briefly, when using a function, the parameters are passed in the array. 4. Use only math.max ()
` ` ` `

Function Findmax4 (Arr) {let result = 0; if (array.isaray (arr)) {result = math.max (… arr); returnrate;} else {console.error (‘the parameter arr is not an array “; return

Explanation:

Based on the last solution, in ES6, there is an expansion operator (…), which can Elements in an array are removed, form a sequence separated by commas. Just satisfying the needs of Math.max () functions. Instead to introduce the knowledge points of JS Number, Math, and arrays

1, Number objects.

Creating a Number object:
` ` ` `
VAR variable = new number

Method 2:

VAR variable = number;

TOSTRING () converts the number into a string of the specified form. (Input digital, convert to a few, number.tostring (2))

TOFIXED () specifies the retained number of digits, and also has a four-round function.
2, Math

Method commonly used method:

CEIL upward

FLOOR () is fetched down

Random () random number method // The resulting pseudo-random number is between 0 and 1 (including 0, no 1),

ROUND round

3, array array object:

Create an array 1:

VAR variable name = new array (); create an array of length 0.

Method 2:

VAR variable name = New Array creates an array object for a specified length.

Method 3:

VAR variable name = New Array (“Element 1”, “Element 2” …); to create an array of objects to array specifying elements.

VAR variable name = [“element 1”, “element 2” …];

Detail to be aware of:

1. The length of the array in JavaScript can change.

VAR arr = new array (); // creates an array object having a length of 0. Arr  = 10; Document.write (“Arr length:” + arr.length + “

“); var arr2 = new array (“Dog Wa”, “Dog Holiday”, “Iron Egg”); Arr2 = [“Dog”, “Dog Hollow”, “Iron Egg”, “Zhang 3”]; Document.write (“Arr2 Length:” + Arr2.length + “

“);

3.1 Method of Digital

A, Concat returns Arr1 to Arr2 array elements to returns a new array. Arr1 = arr1.concat (arr2);

B, Join uses the specified separator to assemble the elements in the array into a string returns.

VAR ELEMENTS = arr1.join (“,”);

C, POP: Remove an array The last element and returns the element. D, PUSH: Add new elements to an array and returns new length values ​​for arrays.
` `  Arr1.push ("Hello");  ` E, REVERSE (); Element of Rouvet Array `
F, SLICE Specifies the beginning index value of the array and the end index value intercepts an element of an array, and returns a sub-array. (Baotou is not included)

VAR SUBARR = Arr1.slice (1, 2);

` ` g, sort sort, sorting must be introduced to the sorting method. (The default is arranged in accordance with the ASCII table) ` `
Arr1 = [19, 1, 20]; Arr1.Sort (SortNumber); // Sort, Sort It must be introduced to the method of sorting. Function SortNumber (NUM1, NUM2) {RETURN NUM1-NUM2;}
H, SPLICE first parameter is the index value of the start to delete elements, the second parameter is deleted element The number of data, the subsequent data is inserted (or may not be written).

Arr1.Splice (1, 1, “Zhang San”, “Li Si”, “Wang 5”);
` ` ` `