From 4324d4e71bc882f60cd5addeb32a8c2c2e11ad38 Mon Sep 17 00:00:00 2001 From: syedjafer Date: Wed, 26 Oct 2022 20:05:37 +0530 Subject: [PATCH 1/3] feat: add calculate median --- Maths/CalculateMedian.ts | 30 ++++++++++++++++++++++++++++++ Maths/test/CalculateMedian.test.ts | 22 ++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 Maths/CalculateMedian.ts create mode 100644 Maths/test/CalculateMedian.test.ts diff --git a/Maths/CalculateMedian.ts b/Maths/CalculateMedian.ts new file mode 100644 index 00000000..e9cfd748 --- /dev/null +++ b/Maths/CalculateMedian.ts @@ -0,0 +1,30 @@ +import { IsEven } from "./IsEven"; + +/** + * @function calculateMedian + * @description This script will find the meadian value of a array of numbers. + * @param {number[]} numbers - Array of numeric values + * @return {number} - median of input numbers + * @see [Median](https://en.wikipedia.org/wiki/Median) + * @example calculateMedian([1, 2, 4, 5, 8]) = 4 + * @example calculateMedian([1, 2, 4, 5]) = 3 + */ + + +export const calculateMedian = (numbers: number[]): number => { + if (numbers.length < 1) { + throw new TypeError("Invalid Input"); + } + + let sortedArray: number[] = numbers.sort((n1,n2) => n1 - n2); + const totalNumbers = sortedArray.length; + + if (IsEven(totalNumbers)){ + let index = (totalNumbers) / 2; + return (sortedArray[index - 1] + sortedArray[index]) / 2; + } else { + let index = (totalNumbers + 1) / 2; + return sortedArray[index - 1]; + } + +}; diff --git a/Maths/test/CalculateMedian.test.ts b/Maths/test/CalculateMedian.test.ts new file mode 100644 index 00000000..57c42f7e --- /dev/null +++ b/Maths/test/CalculateMedian.test.ts @@ -0,0 +1,22 @@ +import { calculateMedian } from "../CalculateMedian"; + +describe("Tests for CalculateMedian", () => { + it("should be a function", () => { + expect(typeof calculateMedian).toEqual("function"); + }); + + it("should throw error for invalid input", () => { + expect(() => calculateMedian([])).toThrow(); + }); + + it("should return the median of an array of numbers - even length", () => { + const medianFunction = calculateMedian([1, 2, 3, 4]); + expect(medianFunction).toBe(2.5); + }); + + it("should return the median of an array of numbers - odd length", () => { + const medianFunction = calculateMedian([1, 2, 3, 4, 6, 8, 9]); + expect(medianFunction).toBe(4); + }); + +}); From 26dbca4ee50fff6176385d4a484a78d49202d0f4 Mon Sep 17 00:00:00 2001 From: syedjafer Date: Thu, 27 Oct 2022 08:05:39 +0530 Subject: [PATCH 2/3] Resolving comments --- Maths/CalculateMedian.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/Maths/CalculateMedian.ts b/Maths/CalculateMedian.ts index e9cfd748..1a350cac 100644 --- a/Maths/CalculateMedian.ts +++ b/Maths/CalculateMedian.ts @@ -1,9 +1,7 @@ -import { IsEven } from "./IsEven"; - /** * @function calculateMedian * @description This script will find the meadian value of a array of numbers. - * @param {number[]} numbers - Array of numeric values + * @param {number[]} numbers - Sorted array of numeric values * @return {number} - median of input numbers * @see [Median](https://en.wikipedia.org/wiki/Median) * @example calculateMedian([1, 2, 4, 5, 8]) = 4 @@ -16,10 +14,9 @@ export const calculateMedian = (numbers: number[]): number => { throw new TypeError("Invalid Input"); } - let sortedArray: number[] = numbers.sort((n1,n2) => n1 - n2); - const totalNumbers = sortedArray.length; + const totalNumbers = numbers.length; - if (IsEven(totalNumbers)){ + if (totalNumbers % 2 === 0){ let index = (totalNumbers) / 2; return (sortedArray[index - 1] + sortedArray[index]) / 2; } else { From e5cd2be77c721de72eef788c55961d663ad7cc18 Mon Sep 17 00:00:00 2001 From: zfl4wless Date: Tue, 14 Mar 2023 08:28:47 +0100 Subject: [PATCH 3/3] feat: improvements & resolves requested changes --- Maths/CalculateMedian.ts | 32 ++++++++++++++---------------- Maths/test/CalculateMedian.test.ts | 31 +++++++++++++++-------------- 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/Maths/CalculateMedian.ts b/Maths/CalculateMedian.ts index 1a350cac..5a6c98bc 100644 --- a/Maths/CalculateMedian.ts +++ b/Maths/CalculateMedian.ts @@ -1,27 +1,25 @@ /** * @function calculateMedian - * @description This script will find the meadian value of a array of numbers. - * @param {number[]} numbers - Sorted array of numeric values - * @return {number} - median of input numbers - * @see [Median](https://en.wikipedia.org/wiki/Median) + * @description This function will find the median value of an array of numbers. + * @param {number[]} numbers Sorted array of numeric values. + * @return {number} The median of input numbers. + * @see https://en.wikipedia.org/wiki/Median * @example calculateMedian([1, 2, 4, 5, 8]) = 4 * @example calculateMedian([1, 2, 4, 5]) = 3 */ - export const calculateMedian = (numbers: number[]): number => { - if (numbers.length < 1) { - throw new TypeError("Invalid Input"); - } - - const totalNumbers = numbers.length; + if (numbers.length < 1) { + throw new TypeError("Input array must contain at least one number."); + } - if (totalNumbers % 2 === 0){ - let index = (totalNumbers) / 2; - return (sortedArray[index - 1] + sortedArray[index]) / 2; - } else { - let index = (totalNumbers + 1) / 2; - return sortedArray[index - 1]; - } + const totalNumbers = numbers.length; + if (totalNumbers % 2 === 0) { + let index = totalNumbers / 2; + return (numbers[index - 1] + numbers[index]) / 2; + } else { + let index = (totalNumbers + 1) / 2; + return numbers[index - 1]; + } }; diff --git a/Maths/test/CalculateMedian.test.ts b/Maths/test/CalculateMedian.test.ts index 57c42f7e..e34a71e1 100644 --- a/Maths/test/CalculateMedian.test.ts +++ b/Maths/test/CalculateMedian.test.ts @@ -1,22 +1,23 @@ import { calculateMedian } from "../CalculateMedian"; describe("Tests for CalculateMedian", () => { - it("should be a function", () => { - expect(typeof calculateMedian).toEqual("function"); - }); + it("should be a function", () => { + expect(typeof calculateMedian).toEqual("function"); + }); - it("should throw error for invalid input", () => { - expect(() => calculateMedian([])).toThrow(); - }); + it("should throw error for invalid input", () => { + expect(() => calculateMedian([])).toThrowError( + "Input array must contain at least one number." + ); + }); - it("should return the median of an array of numbers - even length", () => { - const medianFunction = calculateMedian([1, 2, 3, 4]); - expect(medianFunction).toBe(2.5); - }); - - it("should return the median of an array of numbers - odd length", () => { - const medianFunction = calculateMedian([1, 2, 3, 4, 6, 8, 9]); - expect(medianFunction).toBe(4); - }); + it("should return the median of an array of numbers - even length", () => { + const medianFunction = calculateMedian([1, 2, 3, 4]); + expect(medianFunction).toBe(2.5); + }); + it("should return the median of an array of numbers - odd length", () => { + const medianFunction = calculateMedian([1, 2, 3, 4, 6, 8, 9]); + expect(medianFunction).toBe(4); + }); });