javascript 递归阶乘
javascript factorial with recursion
我正在尝试使用这个简单的代码来计算 5 的阶乘。但我得到的结果是 "undefined"。我知道其他方法,但这有什么问题?
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title> Learning </title>
<head>
<body>
<h2> Welcome<h2>
<p id="demo"></p>
<script>
var fact=5;
function calfact(num)
{
if(num!=1)
{
fact=fact*(num-1);
num=num-1;
calfact(num);
}
else
{
return fact;
}
}
document.getElementById("demo").innerHTML=calfact(5);
</script>
</body>
</html>
如果您想要递归函数的结果,所有 代码路径通过函数必须return 一些东西。在 num!=1
情况下,您的代码没有 returning 任何内容。它应该是 return 调用自身的结果,例如(参见 ***
行):
var fact=5;
function calfact(num)
{
if(num!=1)
{
fact=fact*(num-1);
num=num-1;
return calfact(num); // ***
}
else
{
return fact;
}
}
您的函数正在使用全局变量,这不是一个好主意,因为这意味着该函数不是独立的;并且不是真正的阶乘函数,因为您有效地使用了两个输入(fact
— 全局和 num
,参数)
如果你想要一个真正的阶乘,你不需要一个全局变量,只需从参数本身开始:
function factorial(num) {
if (num < 0) {
throw new Error("num must not be negative");
}
if (num <= 1) {
// Both 1! and 0! are defined as 1
return 1;
}
return num * factorial(num - 1);
}
console.log(factorial(5)); // 120
或者当然,更紧凑:
function factorial(num) {
if (num < 0) {
throw new Error("num must not be negative");
}
return num <= 1 ? 1 : num * factorial(num - 1);
}
(更多关于 0 的信息!:https://en.wikipedia.org/wiki/Factorial)
var fact=5;
function calfact(num){
if(num!=1){
fact=fact*(num-1);
num=num-1;
return calfact(num);//the missing thing
}else{
return fact;//why fact? i think it should be 1
}
}
顺便说一句,你的方法可能有效,但真的很糟糕 style.May 这样做:
function calfact(num){
if(num!=1){
return calfact(num-1)*num;
}else{
return 1;
}
}
或简称:
calfact=num=>num==1?1:calfact(num-1)*num;
我正在尝试使用这个简单的代码来计算 5 的阶乘。但我得到的结果是 "undefined"。我知道其他方法,但这有什么问题?
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title> Learning </title>
<head>
<body>
<h2> Welcome<h2>
<p id="demo"></p>
<script>
var fact=5;
function calfact(num)
{
if(num!=1)
{
fact=fact*(num-1);
num=num-1;
calfact(num);
}
else
{
return fact;
}
}
document.getElementById("demo").innerHTML=calfact(5);
</script>
</body>
</html>
如果您想要递归函数的结果,所有 代码路径通过函数必须return 一些东西。在 num!=1
情况下,您的代码没有 returning 任何内容。它应该是 return 调用自身的结果,例如(参见 ***
行):
var fact=5;
function calfact(num)
{
if(num!=1)
{
fact=fact*(num-1);
num=num-1;
return calfact(num); // ***
}
else
{
return fact;
}
}
您的函数正在使用全局变量,这不是一个好主意,因为这意味着该函数不是独立的;并且不是真正的阶乘函数,因为您有效地使用了两个输入(fact
— 全局和 num
,参数)
如果你想要一个真正的阶乘,你不需要一个全局变量,只需从参数本身开始:
function factorial(num) {
if (num < 0) {
throw new Error("num must not be negative");
}
if (num <= 1) {
// Both 1! and 0! are defined as 1
return 1;
}
return num * factorial(num - 1);
}
console.log(factorial(5)); // 120
或者当然,更紧凑:
function factorial(num) {
if (num < 0) {
throw new Error("num must not be negative");
}
return num <= 1 ? 1 : num * factorial(num - 1);
}
(更多关于 0 的信息!:https://en.wikipedia.org/wiki/Factorial)
var fact=5;
function calfact(num){
if(num!=1){
fact=fact*(num-1);
num=num-1;
return calfact(num);//the missing thing
}else{
return fact;//why fact? i think it should be 1
}
}
顺便说一句,你的方法可能有效,但真的很糟糕 style.May 这样做:
function calfact(num){
if(num!=1){
return calfact(num-1)*num;
}else{
return 1;
}
}
或简称:
calfact=num=>num==1?1:calfact(num-1)*num;