I've been looking and haven't found a simple question and answer on stack overflow looking into finding the average of an array.
This is the array that I have
var grades = [80, 77, 88, 95, 68];
I first thought that the answer to this problem would be something like this:
var avg = (grades / grades.length) * grades.length
console.log(avg)
However this gave me an output of NaN.
So then I tried this:
for ( var i = 0; i < grades.length; i ++){
var avg = (grades[i] / grades.length) * grades.length
}
console.log(avg)
This gave me an output of 68. (I'm not sure why).
So with this I have two questions. 1. Why was my output 68? and 2. Could somebody help me out with actually finding the average of an array?
This question is related to
javascript
arrays
You can use map/reduce functions of javascript to find average. Reduce will sum them up, and map will find average.
var avg = grades.map((c, i, arr) => c / arr.length).reduce((p, c) => c + p);
answer to your 1. question:
for ( var i = 0; i < grades.length; i ++){
var avg = (grades[i] / grades.length) * grades.length
}
avg
is declared in each loop.
Thus, at the end of the loop, avg
has the value of the last item in the array : 68 *5 / 5
The average function you can do is:
const getAverage = (arr) => arr.reduce((p, c) => p + c, 0) / arr.length
Also, I suggest that use the popoular open source tool, eg. Lodash
:
const _ = require('lodash')
const getAverage = (arr) => _.chain(arr)
.sum()
.divide(arr.length)
.round(1)
.value()
One liner challange accepted
const average = arr => arr.reduce((sume, el) => sume + el, 0) / arr.length;
and then
average([1,2,3,4]); // 2.5
Writing the function for average yourself is crazy. That's why modules exist. I found math.js
In my HTML file:
<script src="http://cdnjs.cloudflare.com/ajax/libs/mathjs/3.2.1/math.min.js"></script>
(or you can download it)
In my JS code:
math.mean([1,2,3]) // =2
math.mean(1,2,3) // the same
math.mean(grades)
It can simply be done with a single reduce operation as follows;
var avg = [1,2,3,4].reduce((p,c,_,a) => p + c/a.length,0);_x000D_
console.log(avg)
_x000D_
The MacGyver way,just for lulz
var a = [80, 77, 88, 95, 68];_x000D_
_x000D_
console.log(eval(a.join('+'))/a.length)
_x000D_
There's no built in function, but you can use this to get the sum,
Array.prototype.sum = function() {
return this.reduce(function(a,b){return a+b;});
};
then divide by the arrays length, e.g.:
var arr = [1,2,3,4,5]
console.log(arr.sum() / arr.length)
var total = 0
grades.forEach(function (grade) {
total += grade
});
console.log(total / grades.length)
With ES6 you can turn Andy's solution into as a one-liner:
let average = (array) => array.reduce((a, b) => a + b) / array.length;_x000D_
console.log(average([1,2,3,4,5]));
_x000D_
For the second part of your question you can use reduce
to good effect here:
const grades = [80, 77, 88, 95, 68];_x000D_
_x000D_
function getAvg(grades) {_x000D_
const total = grades.reduce((acc, c) => acc + c, 0);_x000D_
return total / grades.length;_x000D_
}_x000D_
_x000D_
const average = getAvg(grades);_x000D_
console.log(average);
_x000D_
The other answers have given good insight into why you got 68, so I won't repeat it here.
Source: Stackoverflow.com