# Another solution for the Javascript challenge: Factorialize a Number \[SPOILERS\]

**URL:** <https://forum.freecodecamp.org/t/another-solution-for-the-javascript-challenge-factorialize-a-number-spoilers/96498>\
**Category:** General\
**Created:** [March 14, 2017, 5:07am UTC](https://forum.freecodecamp.org/t/another-solution-for-the-javascript-challenge-factorialize-a-number-spoilers/96498 "2017-03-14T05:07:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rafael-atias](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rafael-atias/32/20566_2.png) [@rafael-atias](https://forum.freecodecamp.org/u/rafael-atias)\
**Post date:** [March 14, 2017, 5:07am UTC](https://forum.freecodecamp.org/t/another-solution-for-the-javascript-challenge-factorialize-a-number-spoilers/96498/1 "2017-03-14T05:07:45Z")

</div>

Hello,

I solved this challenge last night, but then I wondered if it could be solved differently. Here it is:

> Factorialize a Number  
> Return the factorial of the provided integer.

> If the integer is represented with the letter n, a factorial is the product of all positive integers less than or equal to n.

> Factorials are often represented with the shorthand notation n!

> For example: 5! = 1 \* 2 \* 3 \* 4 \* 5 = 120

> Remember to use Read-Search-Ask if you get stuck. Write your own code.

And here it is my first (and successful) solution:

```
function factorialize(num) {
  //create an array 
  var array = [];
  //The array must contain every number from 1 to num
  if (num > 0) {
  for(i=1; i<=num; i++) {
    array.push(i);
  }

   // if num = 0, return 1 
  } else {
    return 1;
  }
  
  //Reduction of the array
  return array.reduce(function (a,b) {
    return a * b;
  });
}  

factorialize(5);

```

Later, I wondered if that function could be composed by 2 smaller functions: the first could push the numbers into the array and the second would reduce it. Here is the code I tried:

```
    function factorialize(num) {
  //create an array 
  var array = [];

  //The array must contain every number from 1 to num
 function arrayCreation() {
  if (num > 0) {
  for(i=1; i<=num; i++) {
    array.push(i);
  }

   // if num = 0, return 1 
  } else {
    return 1;
  }
  }

  //Reduction of the array
  function reductionArray() {
  return array.reduce(function (a,b) {
    return a * b;
  });
    }
 arrayCreation();
 reductionArray();
}
factorialize(5);

```

However, when I test the code, the result is `undefined`. I don’t know if it’s a problem caused by the arguments of each function or by the scope of the variable `array`. Any thoughts?

Thanks in advance for your help

---

<div class="post-metadata">

**Author:** ![gunhoo93](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/gunhoo93/32/87162_2.png) [@gunhoo93](https://forum.freecodecamp.org/u/gunhoo93)\
**Post date:** [March 14, 2017, 5:17am UTC](https://forum.freecodecamp.org/t/another-solution-for-the-javascript-challenge-factorialize-a-number-spoilers/96498/2 "2017-03-14T05:17:30Z")

</div>

Your factorialize is not returning anything.

---

<div class="post-metadata">

**Author:** ![rafael-atias](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rafael-atias/32/20566_2.png) [@rafael-atias](https://forum.freecodecamp.org/u/rafael-atias)\
**Post date:** [March 14, 2017, 2:08pm UTC](https://forum.freecodecamp.org/t/another-solution-for-the-javascript-challenge-factorialize-a-number-spoilers/96498/4 "2017-03-14T14:08:58Z")

</div>

@gunhoo93, it was right under my nose…

@rmdawson71 Actually, the solution I showed before was the solution I gave to the challenge. I didn’t even think that the problem could be solved without an array. I will give it a try.

Thank you both for your help

---

<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, 9:39am UTC](https://forum.freecodecamp.org/t/another-solution-for-the-javascript-challenge-factorialize-a-number-spoilers/96498/5 "2021-01-17T09:39:57Z")

</div>

Closed on January 17, 2021, 9:39am UTC


