# Factorialize a number algorithm: Why isn't this working?

**URL:** <https://forum.freecodecamp.org/t/factorialize-a-number-algorithm-why-isnt-this-working/140570>\
**Category:** General\
**Created:** [August 7, 2017, 10:35pm UTC](https://forum.freecodecamp.org/t/factorialize-a-number-algorithm-why-isnt-this-working/140570 "2017-08-07T22:35:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JackBayly](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jackbayly/32/48875_2.png) [@JackBayly](https://forum.freecodecamp.org/u/JackBayly)\
**Post date:** [August 7, 2017, 10:35pm UTC](https://forum.freecodecamp.org/t/factorialize-a-number-algorithm-why-isnt-this-working/140570/1 "2017-08-07T22:35:27Z")

</div>

It keeps on saying that there is a potential infinite loop in the code. Is there something wrong with how I wrote the for loop? Can't figure out why this number isn't factorializing :confused:

```

function factorialize(num) {  
if (num==0){  
return 1;  
}  
for (var a =1;a\<num;a++){  
num+=a;  
}

```
return num;

```

}

factorialize(5);

---

<div class="post-metadata">

**Author:** ![codeman27](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/codeman27/32/21295_2.png) [@codeman27](https://forum.freecodecamp.org/u/codeman27)\
**Post date:** [August 7, 2017, 10:41pm UTC](https://forum.freecodecamp.org/t/factorialize-a-number-algorithm-why-isnt-this-working/140570/2 "2017-08-07T22:41:40Z")

</div>

Hey Jack,

You’re increasing the num size in each loop. num+=a is taking 5 and adding whatever a is to it so a will always be less than num and the loop will never exit.

---

<div class="post-metadata">

**Author:** ![JackBayly](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jackbayly/32/48875_2.png) [@JackBayly](https://forum.freecodecamp.org/u/JackBayly)\
**Post date:** [August 7, 2017, 11:00pm UTC](https://forum.freecodecamp.org/t/factorialize-a-number-algorithm-why-isnt-this-working/140570/3 "2017-08-07T23:00:59Z")

</div>

Thanks for the feedback!! I changed the + to a \* (total d’oh! moment), but I’m still not quite sure why the loop increases the size of num. I think I have to google some more about how loops work 🤔

---

<div class="post-metadata">

**Author:** ![codeman27](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/codeman27/32/21295_2.png) [@codeman27](https://forum.freecodecamp.org/u/codeman27)\
**Post date:** [August 7, 2017, 11:36pm UTC](https://forum.freecodecamp.org/t/factorialize-a-number-algorithm-why-isnt-this-working/140570/4 "2017-08-07T23:36:22Z")

</div>

Loops are hard to wrap your head around unless you see some visual representation of what’s going on.

I’ll try and show what your loop is doing.  
Your loop starts out as:  
for(var a = 1; a \< num; a++)

Beginning for first loop:  
(a \< num) - \> (1 \< 5) --\> True --\> Loop  
(num += a) -\> (num = 5 +1) -\> (num = 6)  
(a++) -\> (a= 1 + 1) -\> (a=2)  
End first loop

Begin Second loop:  
(a \< num) - \> (2 \< 6) --\> True --\> Loop  
(num += a) -\> (num = 6 +2) -\> (num = 8)  
(a++) -\> (a= 2 + 1) -\> (a=3)  
End second loop

Begin Third loop:  
(a \< num) - \> (3 \< 8) --\> True --\> Loop  
(num += a) -\> (num = 8 + 3) -\> (num = 11)  
(a++) -\> (a= 3 + 1) -\> (a=4)  
End third loop

You see how (a \< num) will never = false? That being false is what allows the for loop to exit, if it’s never false it will go on forever or what’s called an infinite loop.

---

<div class="post-metadata">

**Author:** ![codeman27](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/codeman27/32/21295_2.png) [@codeman27](https://forum.freecodecamp.org/u/codeman27)\
**Post date:** [August 7, 2017, 11:52pm UTC](https://forum.freecodecamp.org/t/factorialize-a-number-algorithm-why-isnt-this-working/140570/5 "2017-08-07T23:52:41Z")

</div>

Also, I recommend using Console.log within loops to figure out what’s going on. If you put the below code into your for loop you can see what’s going on in the console (ctrl + shift + i to open Dev Tools in Chrome):

console.log('num: ’ + num + ’ ’ + 'a: ’ + a);

---

<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:46pm UTC](https://forum.freecodecamp.org/t/factorialize-a-number-algorithm-why-isnt-this-working/140570/6 "2021-01-17T20:46:12Z")

</div>

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


