Factorial of a Number error

Factorial of a Number error
0

#1

Tell us what’s happening:
It should have been simple program , i am not able to point out the error.
when i run the program i get : infinity as output for num=5

Constructive criticism appreciated

Your code so far


function factorialize(num) {
  if (num>0){
     for(let i=1; i<num ; i++)
    {
      num=num*i;
    }
    return num;
  }
  else if(num==0){
    return num=1;
  }
  else
  {
   return undefined;
  }
}

console.log(factorialize(5));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/factorialize-a-number/


#2

It’s in an infinite loop because you are comparing i with num but num is always changing.

Also you are actually not actually calculating product of all positive integers, instead you are multiplying given num with i’s instead.

Try using something other than num in your forloop.


#3

understood, thnks that was terrible of me