File tree Expand file tree Collapse file tree 2 files changed +16
-0
lines changed Expand file tree Collapse file tree 2 files changed +16
-0
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' )
18+ }
19+
1220 if ( n === 0 ) {
1321 return 1
1422 }
Original file line number Diff line number Diff 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} )
You can’t perform that action at this time.
0 commit comments