
- 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
Find the average of all elements of array except the largest and smallest - JavaScript
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
- Related Articles
- How to find the Average Values of all the Array Elements in C#?
- Find the difference of largest and the smallest number in an array without sorting it in JavaScript
- How to find all elements in a given array except for the first one using JavaScript?
- Finding the largest and smallest number in an unsorted array of integers in JavaScript
- Find the smallest and second smallest elements in an array in C++
- Python Program To Find the Smallest and Largest Elements in the Binary Search Tree
- Java program to find Largest, Smallest, Second Largest, Second Smallest in an array
- How to find the sum of all elements of a given array in JavaScript?
- Given an array of integers, find the pair of adjacent elements that has the largest product and return that product JavaScript
- How to find the rank of a vector elements in R from largest to smallest?
- C program to find the second largest and smallest numbers in an array
- Find sum of all elements in a matrix except the elements in row and-or column of given cell in Python
- Find smallest and largest elements in singly linked list in C++
- How to find the average of elements of an integer array in C#?
- Picking the largest elements from multidimensional array in JavaScript

Advertisements