0

I'm new to JavaScript and trying to learn closures. There are lots of example using if statements to find the factorial. I'm trying to it do in for loop but the result it's showing as undefined.

My Code:

function factorial(num){

    var num = 5;

    function calculateFactorial(){

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

console.log(factorial());

Where did I get it wrong?

piet.t
  • 11,718
  • 21
  • 43
  • 52

4 Answers4

1

If you want to use closure, then you need return from factorial():

function factorial(foo){ 
    let num = foo;       
    function calculateFactorial(){
        for (let i = num - 1; i >= 1; i--) {
            num = num * i;
        }
        return num;
    }
    return calculateFactorial;
 }

 let myFunc = factorial(10);
 console.log(myFunc());// Output: 3628800

Let me show more simplier and illustrative to me an example:

function fooFunction(foo){ 
    let num = foo;       
    function calculateFactorial(){
        num +=1;
        return num;
    }
    return calculateFactorial;
 }

 let myFunc = fooFunction(1);
 console.log(myFunc()); // Output: 2
 console.log(myFunc()); // Output: 3
 console.log(myFunc()); // Output: 4
 console.log(myFunc()); // Output: 5

Please, read this cool answers at SO about what closure is. These are some highlights:

  • Whenever you use function inside another function, a closure is used.
  • A closure in JavaScript is like keeping a copy of all the local variables, just as they were when a function exited.
  • It is probably best to think that a closure is always created just an entry to a function, and the local variables are added to that closure.
  • A new set of local variables is kept every time a function with a closure is called (given that the function contains a function declaration inside it, and a reference to that inside function is either returned or an external reference is kept for it in some way).
StepUp
  • 36,391
  • 15
  • 88
  • 148
1

Simply, you don't need another function calculateFactorial inside factorial function.

function factorial(num){
    for(i=num-1; i>=1; i--){
        num= num*i; // num *= i
    }
    return num;
}
console.log( factorial(5));
BishalG
  • 1,414
  • 13
  • 24
0

Here is solution : (You need to call function inside also return needed )

function factorial(num_){
   
    var num;
    if (typeof num_ === "undefined") { 
      num = 5;
    } else {
      num = num_;
    }

    var calculateFactorial = function(){

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

console.log(factorial(2));
Nikola Lukic
  • 4,001
  • 6
  • 44
  • 75
0

correct way is:

function factorial(num){

    function calculateFactorial(num){

       for(i=num-1; i>=1; i--){
           num= num*i; // num *= i
       }
       return num;
    }
    return calculateFactorial(num);
}

console.log(factorial(5));

What you're doing is first you are defining a function factorial which takes a num as a variable and inside this you are defining another function to actually calculate the result.So you are complicating the way

Blockquote

Blockquote