File tree Expand file tree Collapse file tree 2 files changed +18
-11
lines changed Expand file tree Collapse file tree 2 files changed +18
-11
lines changed Original file line number Diff line number Diff line change 99 */
1010
1111const 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' )
1814 }
1915
2016 if ( n === 0 ) {
2117 return 1
2218 }
19+
2320 return n * factorial ( n - 1 )
2421}
2522
Original file line number Diff line number Diff line change @@ -10,10 +10,20 @@ describe('Factorial', () => {
1010 } )
1111
1212 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+ )
1828 } )
1929} )
You can’t perform that action at this time.
0 commit comments