-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2fb0d48
commit de27089
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
}) | ||
}) |