From 4324d4e71bc882f60cd5addeb32a8c2c2e11ad38 Mon Sep 17 00:00:00 2001 From: syedjafer Date: Wed, 26 Oct 2022 20:05:37 +0530 Subject: [PATCH 1/2] 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/2] 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 {