File tree 2 files changed +18
-11
lines changed
2 files changed +18
-11
lines changed Original file line number Diff line number Diff line change 9
9
*/
10
10
11
11
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' )
12
+ if ( ! Number . isInteger ( n ) || n < 0 ) {
13
+ throw new RangeError ( 'Input should be a non-negative whole number' )
18
14
}
19
15
20
16
if ( n === 0 ) {
21
17
return 1
22
18
}
19
+
23
20
return n * factorial ( n - 1 )
24
21
}
25
22
Original file line number Diff line number Diff line change @@ -10,10 +10,20 @@ describe('Factorial', () => {
10
10
} )
11
11
12
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' )
13
+ expect ( ( ) => factorial ( '-' ) ) . toThrow (
14
+ 'Input should be a non-negative whole number'
15
+ )
16
+ expect ( ( ) => factorial ( null ) ) . toThrow (
17
+ 'Input should be a non-negative whole number'
18
+ )
19
+ expect ( ( ) => factorial ( undefined ) ) . toThrow (
20
+ 'Input should be a non-negative whole number'
21
+ )
22
+ expect ( ( ) => factorial ( 3.142 ) ) . toThrow (
23
+ 'Input should be a non-negative whole number'
24
+ )
25
+ expect ( ( ) => factorial ( - 1 ) ) . toThrow (
26
+ 'Input should be a non-negative whole number'
27
+ )
18
28
} )
19
29
} )
You can’t perform that action at this time.
0 commit comments