Content-Length: 323590 | pFad | http://github.com/TheAlgorithms/JavaScript/pull/1229/files/e65c9a14a8977df88103fcaf278e97dc45fd0986

A9 chore: check for invalid input to factorial by abdullyahuza · Pull Request #1229 · TheAlgorithms/JavaScript · GitHub
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 (!(typeof n === 'number')) {
throw new TypeError('Not a Number')
}

if ((n % 1) !== 0) {
throw new RangeError('Not a Whole Number')
}

if (n === 0) {
return 1
}
Expand Down
7 changes: 7 additions & 0 deletions Recursive/test/Factorial.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ 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 Number')
expect(() => factorial(null)).toThrow('Not a Number')
expect(() => factorial(undefined)).toThrow('Not a Number')
expect(() => factorial(3.142)).toThrow('Not a Whole Number')
})
})








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/TheAlgorithms/JavaScript/pull/1229/files/e65c9a14a8977df88103fcaf278e97dc45fd0986

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy