Skip to content

chore: check for invalid input to factorial #1229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 27, 2022
8 changes: 8 additions & 0 deletions Recursive/Factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 8 additions & 0 deletions Recursive/test/Factorial.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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