Skip to content

Commit 61ea916

Browse files
committed
Create average_mean.js
1 parent 6960272 commit 61ea916

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

maths/average_mean.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
author: PatOnTheBack
3+
license: GPL-3.0 or later
4+
5+
Modified from:
6+
https://github.com/TheAlgorithms/Python/blob/master/maths/average.py
7+
8+
This script will find the average (mean) of an array of numbers.
9+
10+
More about mean:
11+
https://en.wikipedia.org/wiki/Mean
12+
*/
13+
14+
function mean(nums) {
15+
"use strict";
16+
var sum = 0;
17+
var avg;
18+
19+
// This loop sums all values in the 'nums' array.
20+
nums.forEach(function (current) {
21+
sum += current;
22+
});
23+
24+
// Divide sum by the length of the 'nums' array.
25+
avg = sum / nums.length;
26+
return avg;
27+
}
28+
29+
// Run `mean` Function to find average of a list of numbers.
30+
console.log(mean([2, 4, 6, 8, 20, 50, 70]));

0 commit comments

Comments
 (0)