Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use JavaScript to find the maximum value

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you how to use JavaScript to find the maximum value of the relevant knowledge, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can learn something after reading this article, let's take a look at it.

Methods: 1. Use the "Math.max ()" function to find the maximum value, and return the number with the larger value of the two specified numbers; 2, use the recursive function to find the maximum value; 3, use for loop to find the maximum value; 4, use "Math.max.apply (null, [value, value.)" Statement to find the maximum value.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

The first method

Math.max

JavaScript provides the maximum value returned by the Math.max function in a set of numbers, using:

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

It is worth noting that:

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

Max is a static method for Math, so you should use it like this: Math.max (), not as an instance of Math (simply, without using 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 numeric value, this means that if the parameter can be converted to a number, it can be compared, such as:

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

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

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

After learning about the Math.max method, we take finding the maximum value of an array as an example to think about what can be done to achieve this requirement.

Original method

The most primitive way is to cycle through it:

Var arr = [6, 4, 1, 8, 2, 11, 23]; var result = arr [0]; for (var I = 1; I < arr.length; ionization +) {result = Math.max (result, arr [I]);} console.log (result)

Reduce

Since we are finding a final value by traversing the array, 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 extension operator of ES6:

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

Use recursive functions

Var arr = [9, 8, 5, 5, 6, 6, 49, 68, 109, 5, 5, 3, 3, 6, 6, 6, 1]; var max = arr [0]; function findMax (I) {if (I = = arr.length) return max; if (max < arr [I]) max = arr [I]; findMax (iTun1);} findMax (1); console.log (max)

Traversing using for loops

Var arr = [9, 8, 5, 6, 49, 68, 109, 55, 33, 6, 2, 1]; var max = arr [0]; for (var I = 1; I < arr.length; itemized +) {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, 5, 5, 5, 6, 6, 49, 68, 109, 55, 33, 6, 2, 1])

In addition, there are many array sorting methods, which can obtain the maximum / minimum value based on the new array index value after sorting.

Var a = [1, null 2, 3, 5]; console.log (Math.max.apply (null, a)); / / maximum console.log (Math.min.apply (null, a)); / / minimum

Multi-dimensional arrays can be modified as follows:

Var a = [1Jing 2jue 3, [5je 6], [1je 4je 8]; var ta=a.join (","). Split (","); / / convert to one-dimensional array console.log (Math.max.apply (null,ta)); / / maximum console.log (Math.min.apply (null,ta)); / / minimum is all the content of this article "how to use JavaScript to find the maximum". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report