diff --git a/Data-Structures/Array/MaxMin.js b/Data-Structures/Array/MaxMin.js new file mode 100644 index 0000000000..5c49e8d467 --- /dev/null +++ b/Data-Structures/Array/MaxMin.js @@ -0,0 +1,39 @@ +/** + * @function findMax + * @description Finds the maximum value in an array of numbers + * without using JavaScript's built-in Math.max(). + * @param {number[]} arr - An array of numbers + * @returns {number} - The maximum value + * @see https://en.wikipedia.org/wiki/Maximum_and_minimum + * @example findMax([1, 5, 3, 9, 2]) => 9 + */ +const findMax = (arr) => { + if (!Array.isArray(arr) || arr.length === 0) { + throw new TypeError('Input must be a non-empty array') + } + if (!arr.every((n) => typeof n === 'number' && !isNaN(n))) { + throw new TypeError('All elements must be numbers') + } + return arr.reduce((max, n) => (n > max ? n : max), arr[0]) +} + +/** + * @function findMin + * @description Finds the minimum value in an array of numbers + * without using JavaScript's built-in Math.min(). + * @param {number[]} arr - An array of numbers + * @returns {number} - The minimum value + * @see https://en.wikipedia.org/wiki/Maximum_and_minimum + * @example findMin([1, 5, 3, 9, 2]) => 1 + */ +const findMin = (arr) => { + if (!Array.isArray(arr) || arr.length === 0) { + throw new TypeError('Input must be a non-empty array') + } + if (!arr.every((n) => typeof n === 'number' && !isNaN(n))) { + throw new TypeError('All elements must be numbers') + } + return arr.reduce((min, n) => (n < min ? n : min), arr[0]) +} + +export { findMax, findMin } diff --git a/Data-Structures/Array/test/MaxMin.test.js b/Data-Structures/Array/test/MaxMin.test.js new file mode 100644 index 0000000000..f1c50b8ff1 --- /dev/null +++ b/Data-Structures/Array/test/MaxMin.test.js @@ -0,0 +1,53 @@ +import { findMax, findMin } from '../MaxMin.js' + +describe('findMax', () => { + test('findMax([1,5,3,9,2]) returns 9', () => { + expect(findMax([1, 5, 3, 9, 2])).toBe(9) + }) + + test('findMax single element returns that element', () => { + expect(findMax([5])).toBe(5) + }) + + test('findMax with negative numbers', () => { + expect(findMax([-3, -1, -5])).toBe(-1) + }) + + test('findMax with all same values', () => { + expect(findMax([3, 3, 3])).toBe(3) + }) + + test('throws TypeError for empty array', () => { + expect(() => findMax([])).toThrow(TypeError) + }) + + test('throws TypeError for non-array input', () => { + expect(() => findMax('123')).toThrow(TypeError) + }) +}) + +describe('findMin', () => { + test('findMin([1,5,3,9,2]) returns 1', () => { + expect(findMin([1, 5, 3, 9, 2])).toBe(1) + }) + + test('findMin single element returns that element', () => { + expect(findMin([5])).toBe(5) + }) + + test('findMin with negative numbers', () => { + expect(findMin([-3, -1, -5])).toBe(-5) + }) + + test('findMin with all same values', () => { + expect(findMin([3, 3, 3])).toBe(3) + }) + + test('throws TypeError for empty array', () => { + expect(() => findMin([])).toThrow(TypeError) + }) + + test('throws TypeError for non-array input', () => { + expect(() => findMin('123')).toThrow(TypeError) + }) +})