# New solution I didn't see for this challenge

Hey guys,

I wanted to comment within the “Algorithm Challenge Guide: Factorialize Number” and add my solution, but didn’t see an option to do so. I didn’t think any of the current solutions listed or contributed were particularly easy to understand, and wanted to share mine as I found it more readable. You can tell me what you think - or maybe I missed something that would potentially make my code fail that the compiler missed:

`````` function factorialize(num) {
var result = 1;

while(num > 0) {
result = result * num;
num--;
}
return result;
}

factorialize(5);``````

It’s a variation the second solution in the guide.

For reference:

``````function factorialize(num) {
for (a = 1;num >= 1; num--) {
a = num * a;
}
return a;
}

factorialize(5);
``````

Differences aside from the names are:

• The `result` variable is initialized in the for loop’s header as `a = 1`.
• The for loop’s condition is `num >= 1`. Since `num` is known to be an integer in both versions, `num >= 1` is synonymous with `num > 0`.
• `num` is decremented in the while loop’s body, but decremented in the for loop header in the for loop.

The loops used are different, but how they do it is essentially the same.

Between the two loops I'd go for the for loop.
``````function factorialize(num) {
var product = 1;
for (var i = 1; i <= num; i++) {
product *= i;
}
return product;
}
``````
1 Like

This can actually be made even shorter.

result = result * num;

can be written as

result *= num;

1 Like