File tree 2 files changed +41
-0
lines changed
2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @function exponentialFunction
3
+ * @description Calculates the n+1 th order Taylor series approximation of exponential function e^x given n
4
+ * @param {Integer } power
5
+ * @param {Integer } order - 1
6
+ * @returns exponentialFunction(2,20) = 7.3890560989301735
7
+ * @url https://en.wikipedia.org/wiki/Exponential_function
8
+ */
9
+ function exponentialFunction ( power , n ) {
10
+ let output = 0
11
+ let fac = 1
12
+ if ( isNaN ( power ) || isNaN ( n ) || n < 0 ) {
13
+ throw new TypeError ( 'Invalid Input' )
14
+ }
15
+ if ( n === 0 ) { return 1 }
16
+ for ( let i = 0 ; i < n ; i ++ ) {
17
+ output += ( power ** i ) / fac
18
+ fac *= ( i + 1 )
19
+ }
20
+ return output
21
+ }
22
+
23
+ export {
24
+ exponentialFunction
25
+ }
Original file line number Diff line number Diff line change
1
+ import { exponentialFunction } from '../ExponentialFunction'
2
+
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' , ( ) => {
13
+ const ex = exponentialFunction ( 5 , 20 )
14
+ expect ( ex ) . toBe ( 148.4131078683383 )
15
+ } )
16
+ } )
You can’t perform that action at this time.
0 commit comments