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