Last active
October 23, 2022 06:48
-
-
Save gsscoder/f060c3318878f3f9c6699f2cb519bfed to your computer and use it in GitHub Desktop.
Typescript script to calculate factorial of a number
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
| // $ 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)) |
Author
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
factorial(0) exceeds call stack.