Factorial of no

Tell us what’s happening:

I’m trying to make a general formula for factorial and implement the same here.
its n! = n((n-1)(n-2)…(n-(n-1)).I can’t proceed any further.
Your code so far

function factorialize(num) {
  for(var i = 0; i < num;i--){
    num * (num -1)*(num -2) .......(I'm stuck here
  return num;


Your browser information:

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

Link to the challenge:

Unless n is less than 0 to start off with, that loop will run infinitely (and will not run in FCC due to it being an infinite loop)

Once you’ve fixed that, what is i on each iteration of the loop? The loop runs over and over again, so you definitely don’t want to write (n - 1) * (n - 2) ... Etc on every iteration else it’s going to run that a bunch of times. n = n * ?;

Hint: You have a variable in your for loop named i. What if instead decrementing i each iteration (i–), you incremented i each iteration (i++) and substituted i for the numbers seen in your equation (1, 2, etc…). Of course, you will need to assign the calculation to a variable (let’s call it product) which will keep the cumulative product.

Pseudo Code

function factorialize(num) {
  var product = ???; // what value should product start with?  
  for(var i = 0; i < num;  i++){
    product =  product * (num - ???);  what should you subtract from num each iteration?
  return num; // what should you return after the for loop has finished? num will just be the original number passed to the function, so this is not correct

There are other ways of solving this, but I tried to keep the majority of your original logic in place.

1 Like

Not a solution but a hint(s)

In the case of factorialize(5); you want this calculation
5((5-1)(5-2)(5-3)(5-4)) which is
5 * 4 * 3 * 2 * 1
5 * 4 = 20
20 * 3 = 60
60 * 2 = 120 and
120 * 1 = 120

Counting up by one (or down by one) is what a for loop does best. Of course if you start at 0 and count down you’ll be getting negative values for i and i will always be < num and your loop will run forever.

Good luck!

1 Like

Its very simple, just keep your i on it.

This worked but I’m still not sure about few things.
-Why var product = 1;
-Can’t we declare var product globally?
function factorialize(num) {
var product=1;
for(var i = 0; i < num;i++){
product = product * (num -i);

return product;


Its very simple, just keep your i on it.

Also in your loop, don’t start with 0 as i's value. Just start with 1.

You are very, very close, just follow my hint, and what i said, and you will get it.

  • the factorial of 0 is 1
  • when you run the tests, if you use a global variable then you will be updating that variable every test. So the first test will work, but then say the first test is factorial of 3, for the next test the start value will be 6 (instead of 1) and so on. Always avoid global variables if you don’t need them because then your functions become unpredictable: they then depend on things that are not under the control of the function.
1 Like

for future reference after you’re done with the course

try to implement pascal’s triangle using triangular arrays in lua

If product were initialized to 0 all of your answers would be 0.
Trying both ways in case of factorialize(5) tracing each iteration of for loop

product = 1  //initialized to 1
product = 1 * (5-0) //product now 5
product  = 5 * (5-1) //product now 20
product = 20 * (5-2) //now 60
product = 60 * (5-3) //now 120
product = 120 * (5-4) //120*1 still 120
return product // 120

product = 0  //initialized to 0
product = 0 * (5-0)  //product is 0
product  = 0 * (5-1)  //product is 0, 0*4 also = 0
product = 0 * (5-2)
product = 0 * (5-3)
product = 0 * (5-4)
return product // 0

You would get the first test correct but the second test would start with product = 120. Clearly not going to get the correct answer for other tests.

Avoid the potential for mistakes. Usually with functions best if all input is parameters, all output is return value and any temporary values should be variables local to function.