Skip to content

Commit 5364a1c

Browse files
authored
chore: check for invalid input to factorial (#1229)
1 parent d9d085f commit 5364a1c

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

Recursive/Factorial.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
*/
1010

1111
const factorial = (n) => {
12+
if (!Number.isInteger(n)) {
13+
throw new RangeError('Not a Whole Number')
14+
}
15+
16+
if (n < 0) {
17+
throw new RangeError('Not a Positive Number')
18+
}
19+
1220
if (n === 0) {
1321
return 1
1422
}

Recursive/test/Factorial.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@ describe('Factorial', () => {
88
it('should return factorial 120 for value "5"', () => {
99
expect(factorial(5)).toBe(120)
1010
})
11+
12+
it('Throw Error for Invalid Input', () => {
13+
expect(() => factorial('-')).toThrow('Not a Whole Number')
14+
expect(() => factorial(null)).toThrow('Not a Whole Number')
15+
expect(() => factorial(undefined)).toThrow('Not a Whole Number')
16+
expect(() => factorial(3.142)).toThrow('Not a Whole Number')
17+
expect(() => factorial(-1)).toThrow('Not a Positive Number')
18+
})
1119
})

0 commit comments

Comments
 (0)
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