Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Create average_mean.js
  • Loading branch information
PatOnTheBack committed Jul 1, 2019
commit 61ea916bd9dd2a27b875d537262c57a90b51f87d
30 changes: 30 additions & 0 deletions maths/average_mean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
author: PatOnTheBack
license: GPL-3.0 or later

Modified from:
https://github.com/TheAlgorithms/Python/blob/master/maths/average.py

This script will find the average (mean) of an array of numbers.

More about mean:
https://en.wikipedia.org/wiki/Mean
*/

function mean(nums) {
"use strict";
var sum = 0;
var avg;

// This loop sums all values in the 'nums' array.
nums.forEach(function (current) {
sum += current;
});

// Divide sum by the length of the 'nums' array.
avg = sum / nums.length;
return avg;
}

// Run `mean` Function to find average of a list of numbers.
console.log(mean([2, 4, 6, 8, 20, 50, 70]));