Skip to content

Instantly share code, notes, and snippets.

@gsscoder
Last active October 23, 2022 06:48
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 gsscoder/f060c3318878f3f9c6699f2cb519bfed to your computer and use it in GitHub Desktop.
Save gsscoder/f060c3318878f3f9c6699f2cb519bfed to your computer and use it in GitHub Desktop.
Typescript script to calculate factorial of a number
// $ tsc factorial.ts && node factorial.js
// 3628800
function factorial(num: number) : number {
if (num == 0) return 1
else return num * factorial(num - 1)
}
console.log(factorial(10))
@saad189
Copy link

saad189 commented Oct 10, 2021

factorial(0) exceeds call stack.

@gsscoder
Copy link
Author

gsscoder commented Oct 12, 2021

factorial(0) exceeds call stack.

Right, added a condition to handle 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment