【发布时间】:2016-05-10 13:27:44
【问题描述】:
我需要在 Taylor 行的帮助下实现递归函数指数函数 (e^x): e^x = 1 + x + x2/2! + x3/3! + ... 但我不明白我做错了什么 我有下一个代码:
function fact(n){
return n * fact(n - 1);
}
function myPow(x, n){
return x * myPow(x, n - 1);
}
function expon(x ,n){
if(n == 1){
return expon(x, n - 1) * x;
}
else{
return expon(x, n - 1) + (myPow(x, n)/fact(n));
}
}
console.log(expon(1, 10));
【问题讨论】:
-
你的阶乘函数没有边界。当
n == 1返回 1 时,您应该在其中包含 if 语句。否则它将无限期地继续。 -
我很确定 expon 函数会无穷大。您需要有另一个 if else 语句,说明当 n = 0 时返回 1,因为 e^0 = 1,然后它将递归地一直返回链。
标签: javascript function recursion taylor-series