Skip to content

Instantly share code, notes, and snippets.

@edwingustafson
Last active May 24, 2018 01:46
Show Gist options
  • Save edwingustafson/0dbcd92dce9ebab25b0664f4e80a3eb4 to your computer and use it in GitHub Desktop.
Save edwingustafson/0dbcd92dce9ebab25b0664f4e80a3eb4 to your computer and use it in GitHub Desktop.
Tail-recursive factorial in TypeScript
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