File tree Expand file tree Collapse file tree 2 files changed +14
-3
lines changed Expand file tree Collapse file tree 2 files changed +14
-3
lines changed Original file line number Diff line number Diff line change @@ -5,10 +5,13 @@ let factorial = 1
5
5
* @description Calculates the n+1 th order Taylor series approximation of exponential function e^x given n
6
6
* @param {Integer } power
7
7
* @param {Integer } order - 1
8
- * @returns exponentialFunction(18,4 ) = 18.4
8
+ * @returns exponentialFunction(2,20 ) = 7.389056098930604
9
9
* @url https://en.wikipedia.org/wiki/Exponential_function
10
10
*/
11
11
function exponentialFunction ( power , n ) {
12
+ if ( isNaN ( power ) || isNaN ( n ) || n < 0 ) {
13
+ throw new TypeError ( 'Invalid Input' )
14
+ }
12
15
if ( n === 0 ) { return 1 }
13
16
const recursion = exponentialFunction ( power , n - 1 )
14
17
powerOfX = powerOfX * power
Original file line number Diff line number Diff line change 1
1
import { exponentialFunction } from '../ExponentialFunction'
2
2
3
- describe ( 'exponentialFunction' , ( ) => {
4
- it ( 'with a power of 5 and order of 21' , ( ) => {
3
+ describe ( 'Tests for exponential function' , ( ) => {
4
+ it ( 'should be a function' , ( ) => {
5
+ expect ( typeof exponentialFunction ) . toEqual ( 'function' )
6
+ } )
7
+
8
+ it ( 'should throw error for invalid input' , ( ) => {
9
+ expect ( ( ) => exponentialFunction ( 2 , - 34 ) ) . toThrow ( )
10
+ } )
11
+
12
+ it ( 'should return the exponential function of power of 5 and order of 21' , ( ) => {
5
13
const ex = exponentialFunction ( 5 , 20 )
6
14
expect ( ex ) . toBe ( 148.4131470673818 )
7
15
} )
You can’t perform that action at this time.
0 commit comments