Question: I'm learning javascript. I have written a none recursive function which I want to return the factorial of a given number.

```
function factorialize(n) {
  for(var i = 0; i < n; i++) {
     n *= i;
  }
  return n;
}

factorialize(5);

```

so result should be `120`

I keep getting `0`

Answer: 
You have two (well, three) mistakes in your code:

First `i` must start from `1` (instead of `0`) and go up to `f - 1` otherwise at the first iteration it will be set to `0` (where `f` is the number you need to factorialized - note I changed the argument of the function)

Second, you cannot use `n` for both the upper bound of the `for` loop and the result being calculated. So you need another variable (here's why I added `f`). 

```
function factorialize(f) {
  var n = f;
  for(var i = 1; i < f; i++) {
     n *= i;
  }
  return n;
}

```

However here (in my above snippet too) there is a **third** error: because factorial of `0` should be 1 awhile the function will return `n` (the `for` loop is skipped).

As factorial is usually expressed as `1 * 2 * 3 * ... * f` I would rewrite all the above this way:

```
function factorialize(f)
{
    var res,
        i;

    res = 1;

    for( i = 1; i <= f, i++ )
    {
        res *= i;
    }

    return res;
}

```

This last function will give you correct result with `factorialize(0)` too.

Eventually the only remainder is check for negative `f`.
You are actually doing this

n = (5 \* 0 \* 1 \* 2\* 3 \* 4 \* 5 \* 6)