diff --git a/Recursive/Factorial.js b/Recursive/Factorial.js index 5d882e5ea4..5a0d560514 100644 --- a/Recursive/Factorial.js +++ b/Recursive/Factorial.js @@ -9,6 +9,14 @@ */ const factorial = (n) => { + if (!Number.isInteger(n)) { + throw new RangeError('Not a Whole Number') + } + + if (n < 0) { + throw new RangeError('Not a Positive Number') + } + if (n === 0) { return 1 } diff --git a/Recursive/test/Factorial.test.js b/Recursive/test/Factorial.test.js index b124efea5e..5f32a44332 100644 --- a/Recursive/test/Factorial.test.js +++ b/Recursive/test/Factorial.test.js @@ -8,4 +8,12 @@ describe('Factorial', () => { it('should return factorial 120 for value "5"', () => { expect(factorial(5)).toBe(120) }) + + it('Throw Error for Invalid Input', () => { + expect(() => factorial('-')).toThrow('Not a Whole Number') + expect(() => factorial(null)).toThrow('Not a Whole Number') + expect(() => factorial(undefined)).toThrow('Not a Whole Number') + expect(() => factorial(3.142)).toThrow('Not a Whole Number') + expect(() => factorial(-1)).toThrow('Not a Positive Number') + }) }) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy