# Help with Factorialize a Number algorithm

**URL:** <https://forum.freecodecamp.org/t/help-with-factorialize-a-number-algorithm/124298>\
**Category:** General\
**Created:** [June 6, 2017, 9:07pm UTC](https://forum.freecodecamp.org/t/help-with-factorialize-a-number-algorithm/124298 "2017-06-06T21:07:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![s1d4rt4](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/s1d4rt4/32/54755_2.png) [@s1d4rt4](https://forum.freecodecamp.org/u/s1d4rt4)\
**Post date:** [June 6, 2017, 9:07pm UTC](https://forum.freecodecamp.org/t/help-with-factorialize-a-number-algorithm/124298/1 "2017-06-06T21:07:49Z")

</div>

help, i have been for two hours trying to figure out how to resolve this… used while, for… but i cant do the last part.  
 ![](https://global.discourse-cdn.com/freecodecamp/original/3X/a/e/ae862d97dea5fafcd3f52dfb22c42a70c7a3888f.png)

this is what i have:  
 ![](https://global.discourse-cdn.com/freecodecamp/original/3X/6/8/6884519b88724661391c9dcbec50e26020e0c9bc.png)

thanks for the help.

---

<div class="post-metadata">

**Author:** ![Soupedenuit](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/soupedenuit/32/32478_2.png) [@Soupedenuit](https://forum.freecodecamp.org/u/Soupedenuit)\
**Post date:** [June 6, 2017, 9:11pm UTC](https://forum.freecodecamp.org/t/help-with-factorialize-a-number-algorithm/124298/2 "2017-06-06T21:11:30Z")

</div>

[https://forum.freecodecamp.com/search?q=factorialize%20a%20number](https://forum.freecodecamp.com/search?q=factorialize%20a%20number)

---

<div class="post-metadata">

**Author:** ![BenGitter](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bengitter/32/12192_2.png) [@BenGitter](https://forum.freecodecamp.org/u/BenGitter)\
**Post date:** [June 6, 2017, 9:22pm UTC](https://forum.freecodecamp.org/t/help-with-factorialize-a-number-algorithm/124298/3 "2017-06-06T21:22:00Z")

</div>

You are not allowed to call `reduce` on an empty array without an initial value. Passing a “initial value” of 1 solves the problem.

```auto
function factorialize(num){
  var array = [];
  for(var i = 1; i <= num; i++){
    array.push(i);
  }
  
  factor = array.reduce(function(a,b){
    return a*b;
  }, 1);
  
  return factor;
}

```

> **[Array.prototype.reduce()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce?v=control)**
>
> The reduce() method applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 8:56pm UTC](https://forum.freecodecamp.org/t/help-with-factorialize-a-number-algorithm/124298/4 "2021-01-17T20:56:18Z")

</div>

Closed on January 17, 2021, 8:56pm UTC


