diff --git a/src/maths/index.ts b/src/maths/index.ts index cf1f616..1e6933e 100644 --- a/src/maths/index.ts +++ b/src/maths/index.ts @@ -1 +1,2 @@ export * from './sum'; +export * from './min'; diff --git a/src/maths/min.test.ts b/src/maths/min.test.ts new file mode 100644 index 0000000..b9fc4da --- /dev/null +++ b/src/maths/min.test.ts @@ -0,0 +1,18 @@ +import min from './min'; + +describe('min', () => { + it('returns the minimum number', () => { + const result = min([2, 1, 3]); + expect(result).toEqual(1); + }); + + it('returns 1 of the minimum number', () => { + const result = min([2, 1, 1]); + expect(result).toEqual(1); + }); + + it('defaults to null when no values are passed in', () => { + const result = min([]); + expect(result).toBeNull(); + }); +}); diff --git a/src/maths/min.ts b/src/maths/min.ts new file mode 100644 index 0000000..f2f9a25 --- /dev/null +++ b/src/maths/min.ts @@ -0,0 +1,6 @@ +export const min = (array: number[]): number | null => { + if (array.length === 0) return null; + return Math.min(...array); +}; + +export default min;