function factorialize(num) {
if (num == 0){
return 1;
}
else{
return num*fact(num-1);
}
factorialize(5);
为什么这段代码不起作用?在这种情况下我甚至可以使用else语句吗?
答案 0 :(得分:2)
您的代码存在两个小问题:
function factorialize(num) {
if (num == 0){
return 1;
}
else{
return num*factorialize(num-1); // 1 - You were calling a non-exister fact function rather than factorialize function
} // 2 - You missed the closing bracket here.
}
function factorialize(num) {
if (num == 0){
return 1;
}
else{
return num*factorialize(num-1);
}
}
console.log(factorialize(5));
答案 1 :(得分:2)
而不是事实(n-1)使用factorialize(n-1)。
答案 2 :(得分:1)
您正在调用不存在的函数:
fact(num-1)
应该在哪里
factorialize(num-1)