From 61ea916bd9dd2a27b875d537262c57a90b51f87d Mon Sep 17 00:00:00 2001 From: PatOnTheBack <51241310+PatOnTheBack@users.noreply.github.com> Date: Mon, 1 Jul 2019 15:07:01 -0400 Subject: [PATCH] Create average_mean.js --- maths/average_mean.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 maths/average_mean.js diff --git a/maths/average_mean.js b/maths/average_mean.js new file mode 100644 index 000000000..4fdcc28dc --- /dev/null +++ b/maths/average_mean.js @@ -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]));