
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Excluding extreme elements from average calculation in JavaScript
We are required to write a JavaScript function that takes in an array of Number. Then the function should return the average of its elements excluding the smallest and largest Number.
Example
The code for this will be −
const arr = [5, 3, 5, 6, 12, 5, 65, 3, 2]; 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
The output in the console −
8.666666666666666
- Related Articles
- JavaScript - Exclude some values in average calculation
- Average of array excluding min max JavaScript
- Pairing an array from extreme ends in JavaScript
- Find consecutive elements average JavaScript
- Cumulative average of pair of elements in JavaScript
- Find the Extreme elements in a List in Java
- Maximum Subarray Sum Excluding Certain Elements in C++
- Sum excluding one element in JavaScript
- Maximum Subarray Sum Excluding Certain Elements in C++ program
- Program to find average salary excluding the minimum and maximum salary in Python
- Get average of every group of n elements in an array JavaScript
- Find longest string in array (excluding spaces) JavaScript
- MongoDB Aggregate to get average from document and of array elements?
- Finding average age from array of Objects using JavaScript
- How to find the mean of a square matrix elements by excluding diagonal elements in R?

Advertisements