1var secondMax = function (){
2 var arr = [20, 120, 111, 215, 54, 78]; // use int arrays
3 var max = Math.max.apply(null, arr); // get the max of the array
4 arr.splice(arr.indexOf(max), 1); // remove max from the array
5 return Math.max.apply(null, arr); // get the 2nd max
6};
7
1var secondMax = function (arr){
2 var max = Math.max.apply(null, arr), // get the max of the array
3 maxi = arr.indexOf(max);
4 arr[maxi] = -Infinity; // replace max in the array with -infinity
5 var secondMax = Math.max.apply(null, arr); // get the new max
6 arr[maxi] = max;
7 return secondMax;
8};
1var secondMax = function (){
2 var arr = [20, 120, 111, 215, 54, 78]; // use int arrays
3 var max = Math.max.apply(null, arr); // get the max of the array
4 arr.splice(arr.indexOf(max), 1); // remove max from the array
5 return Math.max.apply(null, arr); // get the 2nd max
6};
1['20','120','111','215','54','78'].sort(function(a, b) { return b - a; })[1];
2// '120'
3