codewars算法题-Sum without highest and lowest number

算法要求

Sum all the numbers of the array (in F# and Haskell you get a list) except the highest and the lowest element (the value, not the index!).(The highest/lowest element is respectively only one element at each edge, even if there are more than one with the same value!).
If array is empty, null or None, or if only 1 Element exists, return 0

function sumArray(array) {
  if(array==null){
    return 0;
  }else{
    var arrayLen = array.length;
    if(arrayLen==0||arrayLen==1){
      return 0;
    }else {
      var sum = 0;
      for( index in array){
        if(!Number.isNaN(array[index])){
          sum = sum + array[index];
        }
      }
      return sum - Math.max.apply(null, array) - Math.min.apply(null, array);
    }
  }
}

本来不需要加判断数组元素的,多余了。
总结: 求数组极值统一方法: Math.max.apply(null, array);

你可能感兴趣的:(算法学习)