Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit418ac6b

Browse files
make exponentialFunction iterative using a for loop
1 parent7b4ff6e commit418ac6b

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

‎Maths/ExponentialFunction.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
letpowerOfX=1
2-
letfactorial=1
1+
import{calcFactorial}from'./Factorial.js'
2+
33
/**
44
*@function exponentialFunction
55
*@description Calculates the n+1 th order Taylor series approximation of exponential function e^x given n
66
*@param {Integer} power
77
*@param {Integer} order - 1
8-
*@returns exponentialFunction(2,20) = 7.389056098930604
8+
*@returns exponentialFunction(2,20) = 7.3890560989301735
99
*@url https://en.wikipedia.org/wiki/Exponential_function
1010
*/
1111
functionexponentialFunction(power,n){
12+
letoutput=0
1213
if(isNaN(power)||isNaN(n)||n<0){
1314
thrownewTypeError('Invalid Input')
1415
}
1516
if(n===0){return1}
16-
constrecursion=exponentialFunction(power,n-1)
17-
powerOfX=powerOfX*power
18-
factorial=factorial*n
19-
returnrecursion+powerOfX/factorial
17+
for(leti=0;i<n;i++){
18+
output+=(power**i)/calcFactorial(i)
19+
}
20+
returnoutput
2021
}
2122

2223
export{

‎Maths/test/ExponentialFunction.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ describe('Tests for exponential function', () => {
1111

1212
it('should return the exponential function of power of 5 and order of 21',()=>{
1313
constex=exponentialFunction(5,20)
14-
expect(ex).toBe(148.4131470673818)
14+
expect(ex).toBe(148.4131078683383)
1515
})
1616
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp