Problem from Hackerrank.
My solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
function miniMaxSum(arr) { let minValue, maxValue, minIndex, maxIndex, minSum = 0, maxSum = 0; minValue = Math.min(...arr); maxValue = Math.max(...arr); minIndex = arr.indexOf(minValue); maxIndex = arr.indexOf(maxValue); for ( let i = 0; i < arr.length; i++) { if ( minIndex !== i ) { maxSum += arr[i]; } if ( maxIndex !== i ) { minSum += arr[i]; } } console.log(minSum, maxSum); } |