Last active
August 29, 2015 13:56
-
-
Save bradoyler/9156640 to your computer and use it in GitHub Desktop.
JS console lesson : recursive math
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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