FreeCodeCamp: Basic Algorithm Scripting- Factorialize a Number Solution
Factorialize a number
Jul 23, 2017 · 1 min read
Here’s the Solution Link - https://www.freecodecamp.org/meetzaveri
function factorialize(num) {
var res=[];
var product = 1;
var restotal;
for(j=1;j<=num;j+=2){
// Done Factorial math here pushed result in resultunt array
res.push(j*(j-1));
}
for(i=0;i<res.length;i++){
// If zero here then do nothing
if(res[i] === 0){
}
// if it contains something then iterate for storing value as 1*(2)=2 then
// 2*(3) =6 then 6*(4) =24 as it goes on… till it exits from loop
else
product *= res[i];
}
resodd=product; // Result stored for odd input(s)
reseven=product*num; // Result stored for even input(s)
reszero=1; // Result stored for zero input
if(num === 0){
return reszero;
}
else if(num % 2 === 0)
{
return reseven;
}
else
{
return resodd;
}
}factorialize(10);
console.log(factorialize(4));
