Home >Web Front-end >JS Tutorial >How to find the maximum value in JavaScript

How to find the maximum value in JavaScript

青灯夜游
青灯夜游Original
2021-06-23 17:34:2018607browse

Method: 1. Use the "Math.max()" function to find the maximum value, which can return the number with the larger value among the two specified numbers; 2. Use the recursive function to find the maximum value; 3. Use a for loop to traverse to find the maximum value; 4. Use the "Math.max.apply(null, [value, value, value, value...)" statement to find the maximum value.

How to find the maximum value in JavaScript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

The first method

Math.max

JavaScript provides the Math.max function to return a set of numbers Maximum value, usage is:

Math.max([value1[,value2, ...]])

It is worth noting:

  • If any parameter cannot be converted to a numerical value, the result is NaN.

  • max is a static method of Math, so it should be used like this: Math.max(), not as a method of Math instance (in simple terms, do not use new)

  • If there are no parameters, the result is -Infinity (note that it is negative infinity)

And what we need to analyze is:

1. If any parameter cannot be converted to a number, this means that if the parameter can be converted to a number, it can be compared, such as:

Math.max(true, 0) // 1
Math.max(true, '2', null) // 2
Math.max(1, undefined) // NaN
Math.max(1, {}) // NaN

2. If there are no parameters, the result is - Infinity, corresponding to the Math.min function, if there are no parameters, the result is Infinity, so:

var min = Math.min();
var max = Math.max();
console.log(min > max);

Understanding the Math.max method, we take the example of finding the maximum value of an array to think about what methods can be used fulfill this requirement.

Original method

The most primitive method is to loop through it again:

var arr = [6, 4, 1, 8, 2, 11, 23];

var result = arr[0];
for (var i = 1; i < arr.length; i++) {
    result =  Math.max(result, arr[i]);
}
console.log(result);

reduce
Since It is to find a final value by traversing the array, then we can use the reduce method:

var arr = [6, 4, 1, 8, 2, 11, 23];

function max(prev, next) {
    return Math.max(prev, next);
}
console.log(arr.reduce(max));

apply

Using apply is another way.

var arr = [6, 4, 1, 8, 2, 11, 23];
console.log(Math.max.apply(null, arr))

ES6 …

Use the ES6 spread operator:

var arr = [6, 4, 1, 8, 2, 11, 23];
console.log(Math.max(...arr))

Second method

Use recursive functions

var arr = [9,8,55,66,49,68,109,55,33,6,2,1];
var max = arr[0];
function findMax( i ){
  if( i == arr.length ) return max;
  if( max < arr[i] ) max = arr[i];
  findMax(i+1);
}
 
findMax(1);
console.log(max);

Use for loop traversal

var arr = [9,8,55,66,49,68,109,55,33,6,2,1];  
var max = arr[0];
for(var i = 1; i < arr.length; i++){
  if( max < arr[i] ){
    max = arr[i];
  }
}
 
console.log(max);

Use apply to pass the array into the max method and return it directly

Math.max.apply(null,[9,8,55,66,49,68,109,55,33,6,2,1])

In addition, there are many array sorting Either way, you can get the maximum/minimum value based on the new array index value after sorting.

var a=[1,2,3,5];
console.log(Math.max.apply(null, a));//最大值
console.log(Math.min.apply(null, a));//最小值

Multidimensional arrays can be modified like this:

var a=[1,2,3,[5,6],[1,4,8]];
var ta=a.join(",").split(",");//转化为一维数组
console.log(Math.max.apply(null,ta));//最大值
console.log(Math.min.apply(null,ta));//最小值

[Related recommendations: javascript learning tutorial]

The above is the detailed content of How to find the maximum value in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn