mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 17:50:39 +08:00
merge: Add FindMin (#849)
This commit is contained in:
23
Maths/FindMin.js
Normal file
23
Maths/FindMin.js
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/**
|
||||||
|
* @function FindMin
|
||||||
|
* @description Function to find the minimum number given in an array of integers.
|
||||||
|
* @param {Integer[]} nums - Array of Integers
|
||||||
|
* @return {Integer} - The minimum number of the array.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const findMin = (...nums) => {
|
||||||
|
if (nums.length === 0) {
|
||||||
|
throw new TypeError('Array is empty')
|
||||||
|
}
|
||||||
|
|
||||||
|
let min = nums[0]
|
||||||
|
for (let i = 1; i < nums.length; i++) {
|
||||||
|
if (nums[i] < min) {
|
||||||
|
min = nums[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return min
|
||||||
|
}
|
||||||
|
|
||||||
|
export { findMin }
|
18
Maths/test/FindMin.test.js
Normal file
18
Maths/test/FindMin.test.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import { findMin } from '../FindMin'
|
||||||
|
|
||||||
|
describe('FindMin', () => {
|
||||||
|
test('Should return the minimum number in the array', () => {
|
||||||
|
const min = findMin(2, 5, 1, 12, 43, 1, 9)
|
||||||
|
expect(min).toBe(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('Should return the minimum number in the array', () => {
|
||||||
|
const min = findMin(21, 513, 6)
|
||||||
|
expect(min).toBe(6)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('Should throw error', () => {
|
||||||
|
const min = () => findMin()
|
||||||
|
expect(min).toThrow('Array is empty')
|
||||||
|
})
|
||||||
|
})
|
Reference in New Issue
Block a user