HEAD
Task Sum all the numbers of a given array ( cq. list ), except the highest and the lowest element ( by value, not by index! ). The highest or lowest element respectively is a single element at each edge, even if there are more than one with the same value. Input validation If an empty value ( null, None, Nothing etc. ) is given instead of an array, or the given array is an empty list or a list with only 1 element, return 0.
Example { 6, 2, 1, 8, 10 } => 16 { 1, 1, 11, 2, 3 } => 6
function sumArray(array) {
return array == null || array.length <= 1 ? 0 : array.sort((a,b) => a-b).slice(1, array.length - 1).reduce((a,b) => a + b, 0)
}
OR
// in a conditional
// if (array == null || array.length <= 1){
// return 0
// }
// else{
// return array.sort((a,b) => a-b).slice(1, array.length - 1).reduce((a,b) => a + b, 0)
// }
//}
Try it yourself - CodeWars
Task Sum all the numbers of a given array ( cq. list ), except the highest and the lowest element ( by value, not by index! ). The highest or lowest element respectively is a single element at each edge, even if there are more than one with the same value. Input validation If an empty value ( null, None, Nothing etc. ) is given instead of an array, or the given array is an empty list or a list with only 1 element, return 0.
Example { 6, 2, 1, 8, 10 } => 16 { 1, 1, 11, 2, 3 } => 6
function sumArray(array) {
return array == null || array.length <= 1 ? 0 : array.sort((a,b) => a-b).slice(1, array.length - 1).reduce((a,b) => a + b, 0)
}
OR
// in a conditional
// if (array == null || array.length <= 1){
// return 0
// }
// else{
// return array.sort((a,b) => a-b).slice(1, array.length - 1).reduce((a,b) => a + b, 0)
// }
//}
Try it yourself - CodeWars