Skip to content

Commit de27089

Browse files
merge: Add FindMin (TheAlgorithms#849)
1 parent 2fb0d48 commit de27089

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

Maths/FindMin.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @function FindMin
3+
* @description Function to find the minimum number given in an array of integers.
4+
* @param {Integer[]} nums - Array of Integers
5+
* @return {Integer} - The minimum number of the array.
6+
*/
7+
8+
const findMin = (...nums) => {
9+
if (nums.length === 0) {
10+
throw new TypeError('Array is empty')
11+
}
12+
13+
let min = nums[0]
14+
for (let i = 1; i < nums.length; i++) {
15+
if (nums[i] < min) {
16+
min = nums[i]
17+
}
18+
}
19+
20+
return min
21+
}
22+
23+
export { findMin }

Maths/test/FindMin.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { findMin } from '../FindMin'
2+
3+
describe('FindMin', () => {
4+
test('Should return the minimum number in the array', () => {
5+
const min = findMin(2, 5, 1, 12, 43, 1, 9)
6+
expect(min).toBe(1)
7+
})
8+
9+
test('Should return the minimum number in the array', () => {
10+
const min = findMin(21, 513, 6)
11+
expect(min).toBe(6)
12+
})
13+
14+
test('Should throw error', () => {
15+
const min = () => findMin()
16+
expect(min).toThrow('Array is empty')
17+
})
18+
})

0 commit comments

Comments
 (0)