Last active
May 24, 2018 01:46
-
-
Save edwingustafson/0dbcd92dce9ebab25b0664f4e80a3eb4 to your computer and use it in GitHub Desktop.
Tail-recursive factorial in TypeScript
This file contains hidden or 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
function factorial( | |
n: number, | |
accumulator: number = 1 | |
): number { | |
if(n === 1) | |
return accumulator | |
return factorial(n - 1, n * accumulator) | |
} | |
console.log(factorial(5)) // yields 120 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment