# How to obtain average value of all the elements that are greater than a particular value in an Array (Javascript)?

Suppose an array consists numbers only. We want to calculate the average value of all the numbers in the array that are greater than a particular value. Lets see how can we achieve this using Javascript.

**Example**

**Suppose we want to calculate the average value of all the numbers that are greater than 20.**

In the following, numlist array contains numbers.

Our first job would be to filter out all the numbers that are greater than 20 and form a new array. Then we can easily calculate the average value.

<script type="text/javascript">

var numlist=[5,10,15,20,25,30,35,40];

var newNumList=numlist.filter(element => element > 20);

//newNumList contains all the values that are greater than 20.

//calculate the average

var totalSum=0;

for(var iNum=0;iNum<newNumList.length;iNum++){

totalSum+=newNumList[iNum];

}

var averageSum=totalSum/newNumList.length;

console.log(averageSum);

</script>

How to obtain average value of all the elements that are greater than a particular value in an Array (Javascript)?
Reviewed by Pixel Stream
on
December 17, 2019
Rating:

## No comments: