Last active
August 29, 2015 13:58
-
-
Save willmcneilly/10006033 to your computer and use it in GitHub Desktop.
Find factorial by iteration, use one return statement in iterator
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="Find fctorial by iteration, use one return statement in iterator" /> | |
<meta charset="utf-8"> | |
<title>Find factorial by functional iteration</title> | |
</head> | |
<body> | |
</body> | |
</html> |
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
// Finding a factorial by iteration. multiply 1 by 2 take the result and multiply by three until we reach n. | |
function factorial(n) { | |
return factIter(1, 1, n); | |
} | |
function factIter(product, counter, maxCount) { | |
return counter > maxCount | |
? product | |
: factIter(product * counter, counter + 1, maxCount); | |
} | |
console.log(factorial(5)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment