Question: Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.

For example `var arr = [1,3,5,7,9]` the output will be 16 24.

Here is my solution.everything works except one case.When all element in the arr are equal my solutions returns error. How I can fix it?

```
function miniMaxSum(arr) {
    let largest = arr[0];
    let smallest = arr[0];
    let largestSum = 0;
    let smallestSum = 0;
    for(let i = 0; i < arr.length; i ++){
      if(arr[i] > largest){
        largest = arr[i];
      }
      if (arr[i] < smallest){
        smallest = arr[i];
      }
    }
    for(let j = 0; j < arr.length; j ++){
      if(arr[j] < largest){
        smallestSum = smallestSum + arr[j];
      }
      if(arr[j] > smallest){
        largestSum = largestSum + arr[j];
      }
    }
    console.log(smallestSum + " " + largestSum)
}

```

Answer: 
### `Using ES6:`

```js
let numbers = [3,1,5,9,7]

let ascending = JSON.parse(JSON.stringify(numbers)).sort((a, b) => a - b)
ascending.pop()
let min = ascending.reduce((a, b) => a + b)

let descending = JSON.parse(JSON.stringify(numbers)).sort((a, b) => b - a)
descending.pop()
let max = descending.reduce((a, b) => a + b)

console.log(`${min} ${max}`)
```

OR

```js
let numbers = [3,1,5,9,7]

let sum = numbers.reduce((a, b) => a + b)

let maxNumber = Math.max(...numbers)
let minNumber = Math.min(...numbers)

console.log(`${sum - maxNumber} ${sum - minNumber}`)
```
**New Answer**

```js
const arr = [5,5,5,5,7]

const min = (arr) => {
    const max = Math.max(...arr)
    const min = Math.min(...arr)
    let maxIndexValue
    let minIndexValue

    let maxResult = 0
    let minResult = 0

    if(arr.reduce((val, c) => val + c) / arr.length === arr[0]){
        const arrayFilter = arr.slice(1);

        let maxResult = arrayFilter.reduce((val, c) => val + c)
        let minResult = arrayFilter.reduce((val, c) => val + c)

        console.log(maxResult, minResult)

    }else{
        for(let i = 0; i < arr.length; i++){
            if(arr[i] === max){
                maxIndexValue = i
            }
            if(arr[i] === min){
                minIndexValue = i
            }
        }

        const maxArray = arr.filter((element, index, num)=> num[index] !== num[maxIndexValue])
        const minArray = arr.filter((element, index, num)=> num[index] !== num[minIndexValue])

        const maxResult = maxArray.reduce((val, c) => val + c)
        const minResult = minArray.reduce((val, c) => val + c)

        console.log(maxResult, minResult)
    }

}

min(arr)
```