1

I'm trying to solve the task (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!))

This is my code but I am getting NaN

function sumArray(array) {
  var sum;
  array.sort(function(a, b) {
    return a - b
  });
  for (var i = 1; i < array.length - 2; i++) {
    return sum += array[i];
  }

}
console.log(
  sumArray([6, 2, 1, 8, 10])
)  
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Elder
  • 341
  • 3
  • 8
  • 21

2 Answers2

1

Some issues:

  • sum needs an initial value, i.e. 0
  • You return inside the loop, so the loop can only do one iteration, ... ever. Move it after the loop
  • You are not summing the one but last (ordered) value. Use i < array.length - 1
  • Your algorithm relies on sort which gives it a time of complexity of O(nlogn). You can do this in O(n) if you use Math.min and Math.max:

function sumArray(array) {
    return array.length < 2 ? 0
         : array.reduce((a, b) => a + b) - Math.min(...array) - Math.max(...array);
}
console.log(
    sumArray([6, 2, 1, 8, 10])
)  

NB: This is ES6 code, supported by all main browsers, with IE being one notable exception.

For older browsers:

function sumArray(array) {
    return array.length < 2 ? 0
         : array.reduce(function (a, b) {
               return a + b;
           }) - Math.min.apply(null, array) - Math.max.apply(null, array);
}
console.log(
    sumArray([])
)  

For very large arrays it may be necessary to get minimum and maximum values via reduce as the number of arguments that can be passed to a function call is limited.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • This however is ES5+. A bit over the head of OP I would think and not running on all browsers – mplungjan Feb 17 '18 at 15:06
  • 1
    @mplungjan She did not specify that she needed her code to be corrected, she outlined the task, and asked for a solution. – doubleOrt Feb 17 '18 at 15:13
  • There is this though: `However, both spread (...) and apply will either fail or return the wrong result if the array has too many elements, because they try to pass the array elements as function parameters.` https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max – mplungjan Feb 17 '18 at 15:25
0
  1. initialise the sum=0
  2. move the return to outside the loop
  3. loop until <length-1, not 2

Note in the second example I use reduce to get max and min since

both spread (...) and apply will either fail or return the wrong result if the array has too many elements, because they try to pass the array elements as function parameters.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max

function sumArray(array) {
  var sum = 0;
  array.sort(function(a, b) {
    return a - b
  });
  for (var i = 1; i < array.length - 1; i++) {
    sum += array[i];
  }
  return sum;
}
console.log(
  sumArray([6, 2, 1, 8, 10])
)

// or not sorting
let arr = [6, 2, 1, 8, 10];
const max = arr.reduce(function(a, b) {
  return Math.max(a, b);
})
const min = arr.reduce(function(a, b) {
  return Math.min(a, b);
})
let sum = arr.reduce(function(a, b) {
  return a + b;
}, 0); // add 
sum -= (min + max);
console.log(sum)

// using one reduce (https://stackoverflow.com/a/43576388/295783):
let minmax = arr.reduce(
  (accumulator, currentValue) => {
    return [
      Math.min(currentValue, accumulator[0]),
      Math.max(currentValue, accumulator[1])
    ];
  }, [Number.MAX_VALUE, Number.MIN_VALUE]
)
sum = arr.reduce(function(a, b) {
  return a + b;
}, 0); // add 
sum -= (minmax[0] + minmax[1]);
console.log(sum)
mplungjan
  • 169,008
  • 28
  • 173
  • 236