Skip to content

Commit d2a926e

Browse files
feat: add array findMax and findMin algorithms
1 parent 5c39e87 commit d2a926e

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

‎Data-Structures/Array/MaxMin.js‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @function findMax
3+
* @description Finds the maximum value in an array of numbers
4+
* without using JavaScript's built-in Math.max().
5+
* @param {number[]} arr - An array of numbers
6+
* @returns {number} - The maximum value
7+
* @see https://en.wikipedia.org/wiki/Maximum_and_minimum
8+
* @example findMax([1, 5, 3, 9, 2]) => 9
9+
*/
10+
const findMax = (arr) => {
11+
if (!Array.isArray(arr) || arr.length === 0) {
12+
throw new TypeError('Input must be a non-empty array')
13+
}
14+
if (!arr.every((n) => typeof n === 'number' && !isNaN(n))) {
15+
throw new TypeError('All elements must be numbers')
16+
}
17+
return arr.reduce((max, n) => (n > max ? n : max), arr[0])
18+
}
19+
20+
/**
21+
* @function findMin
22+
* @description Finds the minimum value in an array of numbers
23+
* without using JavaScript's built-in Math.min().
24+
* @param {number[]} arr - An array of numbers
25+
* @returns {number} - The minimum value
26+
* @see https://en.wikipedia.org/wiki/Maximum_and_minimum
27+
* @example findMin([1, 5, 3, 9, 2]) => 1
28+
*/
29+
const findMin = (arr) => {
30+
if (!Array.isArray(arr) || arr.length === 0) {
31+
throw new TypeError('Input must be a non-empty array')
32+
}
33+
if (!arr.every((n) => typeof n === 'number' && !isNaN(n))) {
34+
throw new TypeError('All elements must be numbers')
35+
}
36+
return arr.reduce((min, n) => (n < min ? n : min), arr[0])
37+
}
38+
39+
export { findMax, findMin }
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { findMax, findMin } from '../MaxMin.js'
2+
3+
describe('findMax', () => {
4+
test('findMax([1,5,3,9,2]) returns 9', () => {
5+
expect(findMax([1, 5, 3, 9, 2])).toBe(9)
6+
})
7+
8+
test('findMax single element returns that element', () => {
9+
expect(findMax([5])).toBe(5)
10+
})
11+
12+
test('findMax with negative numbers', () => {
13+
expect(findMax([-3, -1, -5])).toBe(-1)
14+
})
15+
16+
test('findMax with all same values', () => {
17+
expect(findMax([3, 3, 3])).toBe(3)
18+
})
19+
20+
test('throws TypeError for empty array', () => {
21+
expect(() => findMax([])).toThrow(TypeError)
22+
})
23+
24+
test('throws TypeError for non-array input', () => {
25+
expect(() => findMax('123')).toThrow(TypeError)
26+
})
27+
})
28+
29+
describe('findMin', () => {
30+
test('findMin([1,5,3,9,2]) returns 1', () => {
31+
expect(findMin([1, 5, 3, 9, 2])).toBe(1)
32+
})
33+
34+
test('findMin single element returns that element', () => {
35+
expect(findMin([5])).toBe(5)
36+
})
37+
38+
test('findMin with negative numbers', () => {
39+
expect(findMin([-3, -1, -5])).toBe(-5)
40+
})
41+
42+
test('findMin with all same values', () => {
43+
expect(findMin([3, 3, 3])).toBe(3)
44+
})
45+
46+
test('throws TypeError for empty array', () => {
47+
expect(() => findMin([])).toThrow(TypeError)
48+
})
49+
50+
test('throws TypeError for non-array input', () => {
51+
expect(() => findMin('123')).toThrow(TypeError)
52+
})
53+
})

0 commit comments

Comments
 (0)