How To Use Recursive Function in TypeScript

Recursive Function in TypeScript

 
A recursive function is a function that calls itself, in other words, multiple times. The advantage of using recursion is code reusability. TypeScript supports creating recursive functions with ease and efficiency. A recursive function allows you to divide the complex problem into identical single simple cases that can be handled easily. This programming technique is called divide and conquer. A recursive function must have at least one exit condition that can be satisfied. Otherwise, the recursive function will call itself forever, in other words, multiple times until the runtime stack overflows.
 
Basically there are two ways to code recursive functions in TypeScript:
  • First by directly calling the function from within itself and
  • Second by using an indirect call
Example
 
In this article, I will use direct recursion. The following example tells you, how to use recursive a function in TypeScript. In this example, we will find the factorial of a number we enter. Do the following to create a program using a recursive function.
 
Step 1
 
Open Visual Studio 2012 and click on "File" -> "New" -> "Project...". After that, a window is opened. Enter the name of your application like "RecursiveFunction", after that click on the Ok button.
 
Step 2
 
After Step 1 your project has been created. The Solution Explorer, which is shown on the right side of your project, contains the js file, ts file, CSS file, and HTML file.
 
Step 3
 
The code of the recursive function program:
 
RecursiveFunction.ts
  1. class Greeter {  
  2.  static fact: number;  
  3.  fact = 1;  
  4.  constructor() {}  
  5.  factorial(num: number): number {  
  6.   if (num > 0) {  
  7.    this.fact = this.fact * num;  
  8.    this.factorial(num - 1);  
  9.   }  
  10.   return this.fact;  
  11.  }  
  12. }  
  13. window.onload = () => {  
  14.  var fact: number, num: number;  
  15.  num = parseInt(prompt("Enter a number"));  
  16.  var greeter = new Greeter();  
  17.  fact = greeter.factorial(num);  
  18.  alert("Factorial of a number is->" + fact);  
  19. };  
Note  In the above-declared program, I have created a recursive function with an argument and it is an example of direct recursion.
 
default.html
  1. <!DOCTYPE html>  
  2. <html lang="en"  
  3.     xmlns="http://www.w3.org/1999/xhtml">  
  4.     <head>  
  5.         <meta charset="utf-8" />  
  6.         <title>TypeScript HTML App</title>  
  7.         <link rel="stylesheet" href="app.css" type="text/css" />  
  8.         <script src="app.js"></script>  
  9.     </head>  
  10.     <body>  
  11.         <h2>Recursion Function in TypeScript</h2>  
  12.         <h3>Number of Factorial By Recursion Function</h3>  
  13.         <div id="content"/>  
  14.     </body>  
  15. </html>  
app.js
  1. var Greeter = (function() {  
  2.  function Greeter() {  
  3.   this.fact = 1;  
  4.  }  
  5.  Greeter.fact = 0;  
  6.  Greeter.prototype.factorial = function(num) {  
  7.   if (num > 0) {  
  8.    this.fact = this.fact * num;  
  9.    this.factorial(num - 1);  
  10.   }  
  11.   return this.fact;  
  12.  };  
  13.  return Greeter;  
  14. })();  
  15. window.onload = function() {  
  16.  var fact;  
  17.  var num;  
  18.  num = parseInt(prompt("Enter a number"));  
  19.  var greeter = new Greeter();  
  20.  fact = greeter.factorial(num);  
  21.  alert("Factorial of a number is->" + fact);  
  22. };  
Output 1
 
first-image-in-type-script.jpg
 
Enter the number and then click on Ok.
 
Output 2
 
 second-image-in-type-script.jpg