D_S - 1 year ago 187
Javascript Question

# Sum without Highest and lower number js

I'm trying to complete a kata inside codewars, but I'm having some problems. I would like to sum all elements inside an array except the highest and lower values.

This is what I have now

``````function sumArray(array) {

let largestVal =  Math.max(array);
let lowestVal =  Math.min(array);
let total =  0;

let arrLength = array.length;

console.log(`The lowest number is: \${lowestVal}`);
console.log(`Array is: \${array}`);
console.log(`Total is: \${total}`);
console.log(`The larget number is: \${largestVal}`);
return  // [QUESTION] <= having problems in how to return the sum of each element except the largest and lowest values.
}
``````

Thanks in advance

EDIT-1: Updated code with @WLatif implementation

``````function validate(array) {
if (typeof array === 'object' && array instanceof Array && array.length > 1) {
return 1;
} else {
return 0;
}
}

function sumArray(array) {
if (validate(array)) {
let sorted = array.sort(function (a, b) { return a - b });
let largestVal = sorted.slice(-1).pop();
let lowestVal =  sorted[0];
let total =  array.reduce( function(prev, curr) { return prev + curr; }, 0 );
let arrLength = array.length;

console.log(`The lowest number is: \${lowestVal}`);
console.log(`Array is: \${array}`);
console.log(`Total is: \${total}`);
console.log(`The larget number is: \${largestVal}`);
return (total- lowestVal - largestVal);
} else {
console.log(`Not a valid array`);
}

}
``````

This is the error I'm getting:

Not a valid array ✘ Expected: 0, instead got: undefined

What am I missing?

Here are the instructions:

``````Sum all the numbers of the array except the highest and the lowest element (the value, not the index!).
(Only one element at each edge, even if there are more than one with the same value!)

Example:

{ 6, 2, 1, 8, 10 } => 16
{ 1, 1, 11, 2, 3 } => 6

If array is empty, null or None, or if only 1 Element exists, return 0.
``````

Answer Source

This can be done by sorting(Ascending order) the array first and then doing the operations neglecting first(smallest) and last(largest) element.
Attached is the snippet
Best of luck.
Edit 1: added validation cases.

``````function validate(array) {
if (typeof array === 'object' && array instanceof Array && array.length > 1) {
return 1;
} else {
return 0;
}
}

function sumArray(array) {
var sorted = array.sort(function(a, b) {
return a - b
});
console.log(`The lowest number is: \${sorted[0]}`);
console.log(`Array is: \${array}`);
console.log(`Total is: \${sorted.reduce(function(a, b) { return a + b; }, 0)}`);
console.log(`The larget number is: \${sorted.slice(-1).pop()}`);
return sorted.slice(1, -1).reduce(function(a, b) {
return a + b;
}, 0);
}

// ideal case
var points = [40, 100, 1, 5, 25, 10];
if (validate(points)) {
console.log(`sum of numbers except smallest and largest number is: \${sumArray(points)}`);
} else {
console.log("Array validation unsuccessful");
}

//empty array case
var points = [];
if (validate(points)) {
console.log(`sum of numbers except smallest and largest number is: \${sumArray(points)}`);
} else {
console.log("Array validation unsuccessful");
}

// single element case
var points = [40];
if (validate(points)) {
console.log(`sum of numbers except smallest and largest number is: \${sumArray(points)}`);
} else {
console.log("Array validation unsuccessful");
}

//other data type case
var points = "x";
if (validate(points)) {
console.log(`sum of numbers except smallest and largest number is: \${sumArray(points)}`);
} else {
console.log("Array validation unsuccessful");
}

//null case
var points = null;
if (validate(points)) {
console.log(`sum of numbers except smallest and largest number is: \${sumArray(points)}`);
} else {
console.log("Array validation unsuccessful");
}``````

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download