Skip to content

Instantly share code, notes, and snippets.

@bradoyler
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradoyler/9156640 to your computer and use it in GitHub Desktop.
Save bradoyler/9156640 to your computer and use it in GitHub Desktop.
JS console lesson : recursive math
// get factorial of a given number
// ie. 4 = (4 * 3 * 2 * 1) = 24
function factorial(x) {
if(x==0) {
return 1;
}
return x * fact(x-1);
}
console.log(factorial(4)); //output 24
// testing for prime
function test_prime(n)
{
if (n===1)
{
return false;
}
else if(n === 2)
{
return true;
}else
{
for(var x = 2; x<n; x++)
{
if(n % x === 0)
{
return false;
}
}
return true;
}
}
console.log(test_prime(37));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment