# Find the average of all elements of array except the largest and smallest - JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

#### JavaScript for beginners

Best Seller

74 Lectures 10 hours

#### Modern Javascript for Beginners + Javascript Projects

Most Popular

112 Lectures 15 hours

#### The Complete Full-Stack JavaScript Course!

Best Seller

96 Lectures 24 hours

We are required to write a JavaScript function that takes in an array of Number and returns the averages of its elements excluding the smallest and largest Number.

Let’s write the code for this function −

Following is the code −

const arr = [1, 4, 5, 3, 5, 6, 12, 5, 65, 3, 2, 65, 9];
const findExcludedAverage = arr => {
const creds = arr.reduce((acc, val) => {
let { min, max, sum } = acc;
sum += val;
if(val > max){
max = val;
};
if(val < min){
min = val;
};
return { min, max, sum };
}, {
min: Infinity,
max: -Infinity,
sum: 0
});
const { max, min, sum } = creds;
return (sum - min - max) / (arr.length / 2);
};
console.log(findExcludedAverage(arr));

## Output

Following is the output in the console −

18.307692307692307