简体   繁体   中英

Why doesn't this give me the factorial?

function FirstFactorial(num) { 
      for(var i = num - 1; i > 0; i--) {
           return num * i; 
      }        
    };

  console.log(FirstFactorial(8))

I just want to know what writing my code like this doesn't print the factorial? Instead I only get 56, which is 8 * 7. I thought that if i use the for loop that it would keep going?

When you use return, it escape the function and returns the first num*i , to prevent it, make a local variable in your function and return that at the end of loop.

Example:

function FirstFactorial(num) {
    var result=1;
    for(var i = num - 1; i > 0; i--) {
        result *= i; 
    }        
    return result
};

You are passing 8 and multiplying 8*7 and return 56. You are not accumulating. You need some recursion here:

function FirstFactorial(num)
{
    if (num == 1)
        return num;
    else
        return num * FirstFactorial(num - 1);
}

Using:

for(var i = num - 1; i > 0; i--) {
    return num * i; 
}

It will return num*(num-1) value. Since it will return the value in the first iteration.

Instead use:

Using recursion:

function FirstFactorial(num) {
    if(num==1)
        return num;
    else
        return num*(FirstFactorial(num-1))
}

or:

fact=num;
for(var i = num - 1; i > 0; i--) {
    fact*=i; 
}
return fact;

EDIT: On demand of OP to explain how the return would work in the recursion method.

Regarding Recursion:

return num*FirstFactorial(num-1)

actually first multiplies num with the return value of FirstFactorial(num-1) which would be (n-1)! . Hence what actually will happen is:

  1. FirstFactorial(num-1) is called. (FirstFactorial function is called with parameter num-1 .
  2. The return value of FirstFactorial(num-1) which is (num-1)! is multiplied with the value of num to get num! .
  3. This value ( num! ) is then returned by the function FirstFactorial() when passed with parameter num or FirstFactorial(num)

Now on calling FirstFactorial(num-1) inside the FirstFactorial(num) , FirstFactorial function will again be executed checking the first if condition. If it fails, it will return the value of (n-1)*FirstFactorial(num-1 - 1) . This will recurse until first if condition is satisfied and value is returned without calling the FirstFactorial again.

You can also think the function as:

function FirstFactorial(num) {
    if(num==1)
        return num;
    else {
        a = FirstFactorial(num-1);
        a = a * num;
        return a;
    }
}

Try something like:

function FirstFactorial(num) { 
  var fact = 1;
  for(var i = num; i > 0; i--) {
       fact = fact * i; //here you are returning on your first iteration iteslef and hence issue
  }
  return fact;        
};

console.log(FirstFactorial(8))

when you say return , it would abort the method call and return to the parent with the value (if it does) that you return. So in your case, it will return 8 * 7 = 56

You return your value at the first time you iterate in your loop, instead of at the end. This should do the trick:

function FirstFactorial(num) { 
      var result = num;
      for(var i = num - 1; i > 0; i--) {
           result = result * i; 
      }
      return result;        
    };

  console.log(FirstFactorial(8))

Your for loop is iterating once because has a return, you should storage in a variable and return after te loop iterates.

function factorial(num) {
    var result = num;

    for (var i = num - 1; i > 0; i--) {
        result = result * i;
    }

    return result;
}

Note factorial is a candidate to be recursive:

function factorial(num) {

    if (num > 0) {
        return num * factorial(num - 1);
    } else {
        return 1;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM