I'm beginner to programming/stackoverflow and trying to create a function that sum elements of an array, without the lowest and highest elements.
So I decided to sort the array using ascending order and splice it to remove first and last values. Figured out that this was going to be the easiest way.
But now I have spent literally hours trying to debug it, watched several youtube videos and tried every array.reduce variant I could imagine and find on the internet, trying to under-****ing-stand array.reduce function and why it is not returning reduced array.
Could someone help me? By the way, if someone knows any ways to splice two values from an array without using array.splice two times, I would love to know. Thank you so much.
function sumArray(array) {
array.sort(function(a,b){return a-b})
console.log(array) //log1
array.splice(0, 1)
array.splice(-1, 1)
array.reduce(function(a,b){return a+b}, 0);
console.log(array) //log2
}
The log results are:
log 1: [ 1, 2, 6, 8, 10 ]
log 2: [ 2, 6, 8 ] instead of 16.