我需要在帮助行泰勒的帮助下实现递归函数指数函数(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));
最佳答案
您的阶乘函数没有基本情况。
function fact(n) {
if (n == 1)
return 1;
if (n < 1)
return 0;
return n * fact(n - 1);
}
myPow
也需要进行类似的更改。虽然幂很有趣,但我认为如果 n == 0 返回 1。
关于javascript - 递归函数 指数函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35141658/