2

Variable min will contain the sum of the 4 smallest items in a given array. Variable max will contain the sum of the 4 largest items in a given array.

JS:

function main() {
  const arr = [1, 2, 3, 4, 5]
  const min = arr.sort().filter((element, index, array) => element !== array[array.length - 1]).reduce((accumulator, currentValue) => {
    return accumulator + currentValue
  })
  const max = arr.sort().filter((element, index, array) => element !== array[0]).reduce((accumulator, currentValue) => {
    return accumulator + currentValue
  })
  console.log(min, max)
}

main()

As expected, [1,2,3,4,5] will result in 10, 14. However, if the given array is [5,5,5,5,5], the program will return TypeError: Reduce of empty array with no initial value.

Why is this?

Thanks.

James Barrett
  • 2,757
  • 4
  • 25
  • 35
  • Why ? Because you have not specified an initial value in .reduce - just put 0 as second parameter to that function. // Also, your filtering logic is not matching goal description you gave - but that's another thing altogether. – c69 Nov 12 '17 at 21:27
  • https://stackoverflow.com/questions/43576241/javascript-using-reduce-to-find-min-and-max-values – c69 Nov 12 '17 at 21:34
  • 1
    I think you forgot to mention that array contains `5` elements? – CodeYogi Nov 12 '17 at 21:50

1 Answers1

3

When all elements are the same, the condition element !== array[array.length - 1] will be false for all elements, because all elements are the same as the last. Therefore the result of the filter(...) will be an empty array, and so you get the error that you got.

In fact this implementation is quite flawed. It's better to work with the index rather than the element values:

function main(arr) {
  const count = 4;

  const sorted = arr.sort((a, b) => a - b);

  const sum = (accumulator, currentValue) => accumulator + currentValue;

  const min = sorted
    .filter((element, index) => index < count)
    .reduce(sum);

  const max = sorted
    .filter((element, index) => index >= arr.length - count)
    .reduce(sum);

  console.log(min, max);
}

main([1, 2, 3, 4, 5]);
main([5, 5, 5, 5, 5]);

I slipped in some other improvements as well:

  • Make the array a parameter of the function, for easier testing
  • Don't sort the array twice, once is enough
  • As @Andrew pointed out in a comment, arr.sort() does not sort integers properly, you need to pass it a comparator function to get the intended effect
  • Reduce duplicated logic: extracted sum function and count variable
  • Replace code block with inlined lambda expression
janos
  • 120,954
  • 29
  • 226
  • 236