From 228711abede547a5e7df4858b50d24bd724c17d2 Mon Sep 17 00:00:00 2001 From: orangegrove_1955 Date: Tue, 4 Oct 2022 10:23:47 +1100 Subject: [PATCH 1/5] Added AverageMean and associated tests to Maths --- Maths/AverageMean.ts | 21 +++++++++++++++++++++ Maths/test/AverageMean.test.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 Maths/AverageMean.ts create mode 100644 Maths/test/AverageMean.test.ts diff --git a/Maths/AverageMean.ts b/Maths/AverageMean.ts new file mode 100644 index 00000000..51e38f79 --- /dev/null +++ b/Maths/AverageMean.ts @@ -0,0 +1,21 @@ +/** + * @function AverageMean + * @description This script will find the mean value of a array of numbers. + * @param {number[]} numbers - Array of numeric values + * @return {number} - mean of input numbers + * @see [Mean](https://en.wikipedia.org/wiki/Mean) + * @example mean([1, 2, 4, 5]) = 3 + * @example mean([10, 40, 100, 20]) = 42.5 + */ + + export const AverageMean = (numbers: number[]) => { + if (!Array.isArray(numbers) || numbers.length < 1) { + throw new TypeError('Invalid Input') + } + + // This loop sums all values in the 'numbers' array using an array reducer + const sum = numbers.reduce((sum, current) => sum + current, 0) + + // Divide sum by the length of the 'numbers' array. + return sum / numbers.length +} \ No newline at end of file diff --git a/Maths/test/AverageMean.test.ts b/Maths/test/AverageMean.test.ts new file mode 100644 index 00000000..5b35d782 --- /dev/null +++ b/Maths/test/AverageMean.test.ts @@ -0,0 +1,31 @@ +import { AverageMean } from '../AverageMean'; + +describe('Tests for AverageMean', () => { + it('should be a function', () => { + expect(typeof AverageMean).toEqual('function') + }) + + it('should throw error for invalid input', () => { + expect(() => AverageMean([])).toThrow() + }) + + it('should return the mean of an array of consecutive numbers', () => { + const meanFunction = AverageMean([1, 2, 3, 4]) + expect(meanFunction).toBe(2.5) + }) + + it('should return the mean of an array of numbers', () => { + const meanFunction = AverageMean([10, 40, 100, 20]) + expect(meanFunction).toBe(42.5) + }) + + it('should return the mean of an array of decimal numbers', () => { + const meanFunction = AverageMean([1.3, 12.67, 99.14, 20]) + expect(meanFunction).toBe(33.2775) + }) + + it('should return the mean of an array of numbers, including negatives', () => { + const meanFunction = AverageMean([10, -40, 100, -20]) + expect(meanFunction).toBe(12.5) + }) + }) \ No newline at end of file From c7f7784ac96d519fae4146253b4ff0bf5b0c0b46 Mon Sep 17 00:00:00 2001 From: orangegrove_1955 Date: Tue, 4 Oct 2022 10:30:04 +1100 Subject: [PATCH 2/5] Added correction to documentation, formatted --- Maths/AverageMean.ts | 26 ++++++++-------- Maths/test/AverageMean.test.ts | 56 +++++++++++++++++----------------- 2 files changed, 41 insertions(+), 41 deletions(-) diff --git a/Maths/AverageMean.ts b/Maths/AverageMean.ts index 51e38f79..d60ca07c 100644 --- a/Maths/AverageMean.ts +++ b/Maths/AverageMean.ts @@ -4,18 +4,18 @@ * @param {number[]} numbers - Array of numeric values * @return {number} - mean of input numbers * @see [Mean](https://en.wikipedia.org/wiki/Mean) - * @example mean([1, 2, 4, 5]) = 3 - * @example mean([10, 40, 100, 20]) = 42.5 + * @example AverageMean([1, 2, 4, 5]) = 3 + * @example AverageMean([10, 40, 100, 20]) = 42.5 */ - export const AverageMean = (numbers: number[]) => { - if (!Array.isArray(numbers) || numbers.length < 1) { - throw new TypeError('Invalid Input') - } - - // This loop sums all values in the 'numbers' array using an array reducer - const sum = numbers.reduce((sum, current) => sum + current, 0) - - // Divide sum by the length of the 'numbers' array. - return sum / numbers.length -} \ No newline at end of file +export const AverageMean = (numbers: number[]) => { + if (!Array.isArray(numbers) || numbers.length < 1) { + throw new TypeError("Invalid Input"); + } + + // This loop sums all values in the 'numbers' array using an array reducer + const sum = numbers.reduce((sum, current) => sum + current, 0); + + // Divide sum by the length of the 'numbers' array. + return sum / numbers.length; +}; diff --git a/Maths/test/AverageMean.test.ts b/Maths/test/AverageMean.test.ts index 5b35d782..14b579cf 100644 --- a/Maths/test/AverageMean.test.ts +++ b/Maths/test/AverageMean.test.ts @@ -1,31 +1,31 @@ -import { AverageMean } from '../AverageMean'; +import { AverageMean } from "../AverageMean"; -describe('Tests for AverageMean', () => { - it('should be a function', () => { - expect(typeof AverageMean).toEqual('function') - }) - - it('should throw error for invalid input', () => { - expect(() => AverageMean([])).toThrow() - }) - - it('should return the mean of an array of consecutive numbers', () => { - const meanFunction = AverageMean([1, 2, 3, 4]) - expect(meanFunction).toBe(2.5) - }) - - it('should return the mean of an array of numbers', () => { - const meanFunction = AverageMean([10, 40, 100, 20]) - expect(meanFunction).toBe(42.5) - }) +describe("Tests for AverageMean", () => { + it("should be a function", () => { + expect(typeof AverageMean).toEqual("function"); + }); - it('should return the mean of an array of decimal numbers', () => { - const meanFunction = AverageMean([1.3, 12.67, 99.14, 20]) - expect(meanFunction).toBe(33.2775) - }) + it("should throw error for invalid input", () => { + expect(() => AverageMean([])).toThrow(); + }); - it('should return the mean of an array of numbers, including negatives', () => { - const meanFunction = AverageMean([10, -40, 100, -20]) - expect(meanFunction).toBe(12.5) - }) - }) \ No newline at end of file + it("should return the mean of an array of consecutive numbers", () => { + const meanFunction = AverageMean([1, 2, 3, 4]); + expect(meanFunction).toBe(2.5); + }); + + it("should return the mean of an array of numbers", () => { + const meanFunction = AverageMean([10, 40, 100, 20]); + expect(meanFunction).toBe(42.5); + }); + + it("should return the mean of an array of decimal numbers", () => { + const meanFunction = AverageMean([1.3, 12.67, 99.14, 20]); + expect(meanFunction).toBe(33.2775); + }); + + it("should return the mean of an array of numbers, including negatives", () => { + const meanFunction = AverageMean([10, -40, 100, -20]); + expect(meanFunction).toBe(12.5); + }); +}); From c5b6c96b412b627b443a5ef8c4a6bc0f237941a5 Mon Sep 17 00:00:00 2001 From: orangegrove_1955 Date: Tue, 4 Oct 2022 10:32:04 +1100 Subject: [PATCH 3/5] Added return type to AverageMean --- Maths/AverageMean.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/AverageMean.ts b/Maths/AverageMean.ts index d60ca07c..0a5b0289 100644 --- a/Maths/AverageMean.ts +++ b/Maths/AverageMean.ts @@ -8,7 +8,7 @@ * @example AverageMean([10, 40, 100, 20]) = 42.5 */ -export const AverageMean = (numbers: number[]) => { +export const AverageMean = (numbers: number[]): number => { if (!Array.isArray(numbers) || numbers.length < 1) { throw new TypeError("Invalid Input"); } From ceecb2a338153d0b567515f21fac19c910d36f8f Mon Sep 17 00:00:00 2001 From: orangegrove_1955 Date: Wed, 5 Oct 2022 22:09:13 +1100 Subject: [PATCH 4/5] Removed unnecessary array check --- Maths/AverageMean.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/AverageMean.ts b/Maths/AverageMean.ts index 0a5b0289..ec684c02 100644 --- a/Maths/AverageMean.ts +++ b/Maths/AverageMean.ts @@ -9,7 +9,7 @@ */ export const AverageMean = (numbers: number[]): number => { - if (!Array.isArray(numbers) || numbers.length < 1) { + if (numbers.length < 1) { throw new TypeError("Invalid Input"); } From 0a85b5136cae9578036c221865ad884fb01190b5 Mon Sep 17 00:00:00 2001 From: orangegrove_1955 Date: Sun, 9 Oct 2022 09:57:44 +1100 Subject: [PATCH 5/5] Renamed from AverageMean to calculateMean --- Maths/{AverageMean.ts => calculateMean.ts} | 8 ++++---- .../{AverageMean.test.ts => calculateMean.test.ts} | 14 +++++++------- 2 files changed, 11 insertions(+), 11 deletions(-) rename Maths/{AverageMean.ts => calculateMean.ts} (74%) rename Maths/test/{AverageMean.test.ts => calculateMean.test.ts} (61%) diff --git a/Maths/AverageMean.ts b/Maths/calculateMean.ts similarity index 74% rename from Maths/AverageMean.ts rename to Maths/calculateMean.ts index ec684c02..e52140cf 100644 --- a/Maths/AverageMean.ts +++ b/Maths/calculateMean.ts @@ -1,14 +1,14 @@ /** - * @function AverageMean + * @function calculateMean * @description This script will find the mean value of a array of numbers. * @param {number[]} numbers - Array of numeric values * @return {number} - mean of input numbers * @see [Mean](https://en.wikipedia.org/wiki/Mean) - * @example AverageMean([1, 2, 4, 5]) = 3 - * @example AverageMean([10, 40, 100, 20]) = 42.5 + * @example calculateMean([1, 2, 4, 5]) = 3 + * @example calculateMean([10, 40, 100, 20]) = 42.5 */ -export const AverageMean = (numbers: number[]): number => { +export const calculateMean = (numbers: number[]): number => { if (numbers.length < 1) { throw new TypeError("Invalid Input"); } diff --git a/Maths/test/AverageMean.test.ts b/Maths/test/calculateMean.test.ts similarity index 61% rename from Maths/test/AverageMean.test.ts rename to Maths/test/calculateMean.test.ts index 14b579cf..5fc5022e 100644 --- a/Maths/test/AverageMean.test.ts +++ b/Maths/test/calculateMean.test.ts @@ -1,31 +1,31 @@ -import { AverageMean } from "../AverageMean"; +import { calculateMean } from "../calculateMean"; describe("Tests for AverageMean", () => { it("should be a function", () => { - expect(typeof AverageMean).toEqual("function"); + expect(typeof calculateMean).toEqual("function"); }); it("should throw error for invalid input", () => { - expect(() => AverageMean([])).toThrow(); + expect(() => calculateMean([])).toThrow(); }); it("should return the mean of an array of consecutive numbers", () => { - const meanFunction = AverageMean([1, 2, 3, 4]); + const meanFunction = calculateMean([1, 2, 3, 4]); expect(meanFunction).toBe(2.5); }); it("should return the mean of an array of numbers", () => { - const meanFunction = AverageMean([10, 40, 100, 20]); + const meanFunction = calculateMean([10, 40, 100, 20]); expect(meanFunction).toBe(42.5); }); it("should return the mean of an array of decimal numbers", () => { - const meanFunction = AverageMean([1.3, 12.67, 99.14, 20]); + const meanFunction = calculateMean([1.3, 12.67, 99.14, 20]); expect(meanFunction).toBe(33.2775); }); it("should return the mean of an array of numbers, including negatives", () => { - const meanFunction = AverageMean([10, -40, 100, -20]); + const meanFunction = calculateMean([10, -40, 100, -20]); expect(meanFunction).toBe(12.5); }); });