FreeCodeCamp: Basic Algorithm Scripting- Factorialize a Number Solution


Factorialize a number

Meet Zaveri
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));
Meet Zaveri

Written by

Frontend Engineer with a keen interest in Computer Science. Passionate about Web performance, React Ecosystem and OSS.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade