Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
41 changes: 19 additions & 22 deletions Maths/AverageMean.js
Original file line number Diff line number Diff line change
@@ -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}
53 changes: 44 additions & 9 deletions Maths/test/AverageMean.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.