How to calculate factorial of a number using recursion in TypeScript

2 Answers

0 votes
function factorial(x : number) : number {
    if (x == 0) {
        return 1;
    }
    else {
        return x * factorial(x - 1);
    }
}
 
const n = 6
 
const fact = factorial(n);
     
console.log(fact);
   

     

     
/*
run:
     
720 
     
*/

 



answered Aug 5, 2022 by avibootz
0 votes
function factorial(x : number) : number {
    return (x == 1 || x == 0) ? 1 : x * factorial(x - 1);
}
 
const n = 6
 
const fact = factorial(n);
     
console.log(fact);
   
     
     
     
/*
run:
     
720 
     
*/

 



answered Aug 5, 2022 by avibootz

Related questions

2 answers 313 views
2 answers 387 views
1 answer 276 views
1 answer 415 views
1 answer 1,115 views
3 answers 509 views
...