Home  >  Article  >  Web Front-end  >  To find the maximum value of an array in js, which method do you use, clever or stupid?

To find the maximum value of an array in js, which method do you use, clever or stupid?

yulia
yuliaOriginal
2018-09-08 17:24:231804browse

Reason for writing this article

The projects I am currently working on rarely use algorithms, so this aspect is naturally a bit unfamiliar. In a recent coding, I encountered the need to obtain the maximum value from an array. At that time, I unconsciously thought of the sort() function of js. Now that I think about it, it is really a "sin". At that time, I felt a little proud in my heart: "Well, I Using the built-in method of js solves a problem that usually needs to be solved by using a sorting algorithm. The code is short and there is no need to write troublesome traversals and comparisons. The readability is good..." The inner drama is heavy, right? Ahem, hey, I'm still young and lazy. The js endogenous sort function also uses sorting. For details, see how the js endogenous sort() function is implemented on segmentfault.

strange skills and tricks
There are many "wonderful skills and tricks" in js, and sometimes I often deliberately use these "wonderful skills and tricks" (note, I am not I am opposed to using it, but sometimes it is not necessary). For example, to find the maximum value in an array, there is no native method for finding the maximum value in Array in js, but Math does:

Math.max(22, 79, 33) // 79

If you want to use an array, you can also use it:

 var arr = [22, 79, 33];
 Math.max.apply(null, arr); // 79

Or instead of using the apply method, you can convert the array into a string and use the eval() method to execute the js code spliced ​​into "Math.max(num1, num2, num3)":

 var max = eval("Math.max(" + arr.join(',') + ")"); // 79

If this You didn't expect that you can just use the sort() function I mentioned above:

var arr = [22, 79, 33];
 var getMax = function(arr) {
   var copyArr = JSON.parse(JSON.stringify(arr));
 var len = copyArr.length;
     copyArr.sort();
    return copyArr[len - 1];
 };
 getMax(arr); // 79

Stupid method
Seeing now, you must be amazed at the power of js. But sometimes relying too much on its "magic skills" will put you in a state of "arrogance". After all, the power of js does not mean that your own coding level is strong. So go back to basics and use stupid methods to exercise your brainpower:

 var arr = [22, 79, 33];
  var getMax = function(arr) {
     var len = arr.length,
         max = arr[0]; 
     while (len--) {
         if (max >= arr[len]) {
            continue;
         }
       max = arr[len];
    } 
    return max;
 };
 getMax(arr); // 79

The above is the detailed content of To find the maximum value of an array in js, which method do you use, clever or stupid?. 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