I’d prefer 1st one as it has proper indentation and is more readable.
There is, however, a non recursive version. By using loops
function factorialize(num) {
var factorial = 1;
for(var i=1;i<=num;i++){
factorial *=i;
}
return factorial ;
}
factorialize(5);
What you do is, you run a loop from 1 (not 0, because anything multiplied by 0 is 0) to i<=num (because you need to include original number as well) or to i < num+1. (for the same boundary condition).
Then inside the loop, you multiply your factorial value with i and return factorial value after loop ends.
Which is preferred?
It really depends on you. For most people recursion is not intuitive.
You can only edit your own posts unless you’re a mod. There isn’t really a point system in the forum, but there are trust levels (see the Trust Level badges).