Skip to content

Instantly share code, notes, and snippets.

@thanklessbastard
Created October 12, 2012 00:36
Show Gist options
  • Select an option

  • Save thanklessbastard/3876645 to your computer and use it in GitHub Desktop.

Select an option

Save thanklessbastard/3876645 to your computer and use it in GitHub Desktop.
javascript find factorial
// question:
// write an function that will find the factorial of a given number
//Loop [Fastest] -------------------------------
function factorial (n) {
var i, pN;
if(n === 0) { return }
else {
pN = 1;
for (i = 1; i <= n; i +=1) {
pN *= i;
}
}
return pN;
}
console.log(factorial(10))
//recursive -------------------------------
function factorial(n) {
if(n == 0) { return }
} else {
return n * factorial(n - 1);
}
}
console.log(factorial(10));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment