function sumArray(array) { if (!array) { return 0; } var len = array.length; if (len < 2 ) { return 0; }
var small = array[0]; var big = array[0]; for (var i = 0; i < len; i++) { array[i] > big ? big = array[i] : big; array[i] < small ? small = array[i] :small; } var total = 0; for (var z = 0; z < len; z++) { total += array[z]; } return total - big - small; }
// 简洁写法 function sumArray(array) { if (array == null) { return 0; } else if (array.length < 2) { return 0; } else { array = array.sort(function(a,b) {return a - b;}); var total = 0; for (var i = 1; i < array.length - 1; i++) { total += array[i]; } return total; } }
function sumArray(array) { if (array == null || array.length <= 2) { return 0 } var max = Math.max.apply(Math, array); var min = Math.min.apply(Math, array); var sum = 0 for (i = 0; i < array.length; i++) { sum += array[i]; } return sum - max - min }