I'm currently taking a course on Free Code Camp and it's asking me return a factorial for any given number. However, I'm kind of stuck on the question (please forgive me, math isn't my strong point, haha). Here's what it asks:
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 = 120f
And here's the starting code:
function factorialize(num) {
return num;
}
factorialize(5);
I'm not looking for a direct answer to the question, but I just really want to know how to start. Thanks for any help in advance!
There is two ways of doing that. First one in the loop and second one is in recursive function.
Loop version is like that:
function factorialize(num) {
for(var answer=1;num>0;num--){
answer*=num;
}
return answer;
}
And recursive one is like that.
function factorialize(num) {
if(num===0){
return 1;
}
else{
return num*factorialize(num-1);
}
}
Both of them will work with all positive integers.