Problem in Factorialize a Number

Problem in Factorialize a Number
0.0 0

#1

want to know whats wrong with my code and why iam not passing the test even iam getting the results exactly right

var fact=1;
function factorialize(num) {
        for(var i =1;i<=num;i++){
          
          fact *=i;
}
        
          return fact;
}

factorialize(0);

#2

For a single test case like factorialize(0) or factorialize(5), your solution does work fine. The problem is you are using a global variable (fact). Why is this a problem? Because the FCC tests are making consecutive calls to your function, the other calls will have access to the last value fact has. For example, if the FCC tests make the following calls,

factorialize(3); // returns 6
factorialize(2); // returns 12, because fact ended as 6 in the previous call to the function
factorialize(5); // returns 1440, because fact ended as 12 in the previous call to the function

This is one reason why it is considered best practice to avoid global variables.


#3

that was pretty helpful … Thanks