Skip to content
Open
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
39 changes: 39 additions & 0 deletions Data-Structures/Array/MaxMin.js
Original file line number Diff line number Diff line change
@@ -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 }
53 changes: 53 additions & 0 deletions Data-Structures/Array/test/MaxMin.test.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
Loading