Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thedom85/d9b2ed63ba396a17d3425f7851af6c7b to your computer and use it in GitHub Desktop.
Save thedom85/d9b2ed63ba396a17d3425f7851af6c7b to your computer and use it in GitHub Desktop.
// https://www.codewars.com/kata/576b93db1129fcf2200001e6/train/javascript
function sumArray(array) {
// --- simple output controls
if(!array) return 0 ;
if(array && Object.keys(array).length <=2) return 0;
// -- find the maximum and the minimum
const max = Math.max(...array);
const min = Math.min(...array);
// -- delete the maximum and the minimum
let index = array.indexOf(max);
array.splice(index, 1);
index = array.indexOf(min);
array.splice(index, 1);
// -- sum
return array.reduce((a, b) => a + b, 0)
}
console.log( sumArray(null) , 0 );
console.log( sumArray([ ]) , 0 );
console.log( sumArray([ 3 ]) , 0 );
console.log( sumArray([ 3, 5 ]) , 0 );
console.log( sumArray([ 6, 2, 1, 8, 10 ]) , 16 );
console.log( sumArray([ 0, 1, 6, 10, 10 ]) , 17 );
console.log( sumArray([ -6, -20, -1, -10, -12 ]), -28);
console.log( sumArray([ -6, 20, -1, 10, -12 ]) , 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment