var ary = [19,23,34,24,39,14,25,36]
1. Sorting method: sort the array (small -> large), the first and last are our maximum and minimum values
ary.sort(function(a,b){
return a-b;
})
var min = ary [0]
var max = ary [ary.length-1]
2. Assumption method: Assume that the first value of the current array is the maximum value, and then compare this value with the following items one by one. If one of the latter values is larger than the assumed value, it means that the assumption is wrong, and we put the assumed value. make a replacement..
Like custom properties, they are the most commonly used programming ideas in Js
var max = ary [0], min = ary [0];
for (var i = 1;i<ary.length;i++) {
var cur = and [i]
cur > max ? cur = max : null;
cur <min? cur = min: nul; l
}
3. Use the max/min method in Math: it is to pass in the piles that need to be compared one by one during execution, so as to get the final result, it is not possible to put the array directly
var min = Math.min (ary); // NaN
console.log(Math.min(19,23,34,24,39,14,25,36)) // 14
"Math.max(" + ary.toString() + ")" // "Math.max(19,23,34,14,25,36)" Don't care about the rest first, let's put the last code we want to execute into a string, and then concatenate the value of each item in the array into the string separately
var max = eval( "Math.max(" + ary.toString() + ")" )
var min = eval( "Math.min(" + ary.toString() + ")" )
Additional knowledge points:
1.eval() turns a string into a JS expression for execution
eval("12+23+34+45") // -> 114
2. Bracket expressions
function fn1() { console.log(1) }
function fn2() { console.log(2) }
var obj = {name:"Zhang San",fn:fn2}
~(fn1,fn2)() // ->2 Only fn2 is executed ->(x1,x2,x3...) Parenthesis expression, multiple contents appear in a parenthesis, separated by ",", but The last result we get is only the last item
~(fn2,obj.fn)(); // obj.fn is executed, but this inside becomes window instead of obj when executed
~(obj.fn)() // this->obj is unchanged if there is only one item of this