diff --git a/Maths/AverageMean.js b/Maths/AverageMean.js index 43f3f8ca00..94e7d07703 100644 --- a/Maths/AverageMean.js +++ b/Maths/AverageMean.js @@ -1,29 +1,26 @@ -'use strict' /* - 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 */ -const mean = (nums) => { - // This is a function returns average/mean of array - let sum = 0 - - // This loop sums all values in the 'nums' array using forEach loop - nums.forEach(function (current) { - sum += current - }) - - // Divide sum by the length of the 'nums' array. - const avg = sum / nums.length - return avg +const mean = (_iterable, _selector = undefined) => { + let sum = 0; + let length = 0; + if (!_selector) { + for (let current of _iterable) { + sum += current; + length++; + } + } + else { + for (let current of _iterable) { + sum += _selector(current); + length++; + } + } + if(length === 0) + return undefined; + return sum/length; } -export { mean } +export {mean} \ No newline at end of file diff --git a/Maths/test/AverageMean.test.js b/Maths/test/AverageMean.test.js index 8b3d7bb132..369f4d119e 100644 --- a/Maths/test/AverageMean.test.js +++ b/Maths/test/AverageMean.test.js @@ -1,11 +1,46 @@ import { mean } from '../AverageMean' -describe('Tests for average mean', () => { - it('should be a function', () => { - expect(typeof mean).toEqual('function') - }) - it('should return the mean of an array of numbers', () => { - const meanFunction = mean([1, 2, 4, 5]) - expect(meanFunction).toBe(3) - }) -}) +describe('math average tests', () => { + // ---------------------------------------------------------------------- + test('should return undefined if given empty object', () => { + expect(mean([])).toBeUndefined(); + }); + // ---------------------------------------------------------------------- + test('should return single valueif given 1 value object', () => { + expect(mean([1])).toBe(1); + expect(mean([-1])).toBe(-1); + expect(mean([0])).toBe(0); + }); + // ---------------------------------------------------------------------- + test('should return correct average in some arrays', () => { + expect(mean([1,2])).toBe(1.5); + expect(mean([9,1,2])).toBe(4); + expect(mean([-1,11])).toBe(5); + expect(mean([0,100])).toBe(50); + expect(mean([100,0])).toBe(50); + expect(mean([100,20,80,0])).toBe(50); + }); + // ---------------------------------------------------------------------- + test('should return undefined if given empty generator', () => { + const src = function*(){} + expect(mean(src())).toBeUndefined(); + }); + // ---------------------------------------------------------------------- + test('should return correct average in generator function', () => { + const src = function*(){ + yield 1; + yield -1; + yield 0; + } + expect(mean(src())).toBe(0); + }); + // ---------------------------------------------------------------------- + test('should return correct average in generator function with custom selector function', () => { + const src = function*(){ + yield "abc"; + yield "de"; + yield "qwertyq"; + } + expect(mean(src(),_x=>_x.length)).toBe(4); + }); +}); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 7bce7f3029..0e55a33528 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,7 +20,7 @@ "devDependencies": { "babel-jest": "^26.3.0", "globby": "^12.0.2", - "jest": "^26.6.3", + "jest": "^26.4.2", "standard": "^16.0.4" } },