From e7a84a7dcec65926e7c0b4eb45fd2e59dd46b713 Mon Sep 17 00:00:00 2001 From: RuSaG0 Date: Thu, 9 Dec 2021 03:05:50 +0300 Subject: [PATCH 1/2] average fn Improved: working with generators, erorr if length is 0, custom selector fn --- Maths/AverageMean.js | 41 ++++++++++++-------------- Maths/test/AverageMean.test.js | 53 ++++++++++++++++++++++++++++------ package-lock.json | 2 +- 3 files changed, 64 insertions(+), 32 deletions(-) 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..93f2503b96 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(average([])).toBeUndefined(); + }); + // ---------------------------------------------------------------------- + test('should return single valueif given 1 value object', () => { + expect(average([1])).toBe(1); + expect(average([-1])).toBe(-1); + expect(average([0])).toBe(0); + }); + // ---------------------------------------------------------------------- + test('should return correct average in some arrays', () => { + expect(average([1,2])).toBe(1.5); + expect(average([9,1,2])).toBe(4); + expect(average([-1,11])).toBe(5); + expect(average([0,100])).toBe(50); + expect(average([100,0])).toBe(50); + expect(average([100,20,80,0])).toBe(50); + }); + // ---------------------------------------------------------------------- + test('should return undefined if given empty generator', () => { + const src = function*(){} + expect(average(src())).toBeUndefined(); + }); + // ---------------------------------------------------------------------- + test('should return correct average in generator function', () => { + const src = function*(){ + yield 1; + yield -1; + yield 0; + } + expect(average(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(average(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" } }, From 32565ede6039ad02728e5767b2dd63412d2e10e4 Mon Sep 17 00:00:00 2001 From: RuSaG0 Date: Thu, 9 Dec 2021 03:09:43 +0300 Subject: [PATCH 2/2] average --- Maths/test/AverageMean.test.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Maths/test/AverageMean.test.js b/Maths/test/AverageMean.test.js index 93f2503b96..369f4d119e 100644 --- a/Maths/test/AverageMean.test.js +++ b/Maths/test/AverageMean.test.js @@ -3,27 +3,27 @@ import { mean } from '../AverageMean' describe('math average tests', () => { // ---------------------------------------------------------------------- test('should return undefined if given empty object', () => { - expect(average([])).toBeUndefined(); + expect(mean([])).toBeUndefined(); }); // ---------------------------------------------------------------------- test('should return single valueif given 1 value object', () => { - expect(average([1])).toBe(1); - expect(average([-1])).toBe(-1); - expect(average([0])).toBe(0); + expect(mean([1])).toBe(1); + expect(mean([-1])).toBe(-1); + expect(mean([0])).toBe(0); }); // ---------------------------------------------------------------------- test('should return correct average in some arrays', () => { - expect(average([1,2])).toBe(1.5); - expect(average([9,1,2])).toBe(4); - expect(average([-1,11])).toBe(5); - expect(average([0,100])).toBe(50); - expect(average([100,0])).toBe(50); - expect(average([100,20,80,0])).toBe(50); + 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(average(src())).toBeUndefined(); + expect(mean(src())).toBeUndefined(); }); // ---------------------------------------------------------------------- test('should return correct average in generator function', () => { @@ -32,7 +32,7 @@ describe('math average tests', () => { yield -1; yield 0; } - expect(average(src())).toBe(0); + expect(mean(src())).toBe(0); }); // ---------------------------------------------------------------------- test('should return correct average in generator function with custom selector function', () => { @@ -41,6 +41,6 @@ describe('math average tests', () => { yield "de"; yield "qwertyq"; } - expect(average(src(),_x=>_x.length)).toBe(4); + expect(mean(src(),_x=>_x.length)).toBe(4); }); }); \ No newline at end of file