Simple Function In TypeScript

Simple Function in TypeScript

 
Function expressions are a powerful feature of TypeScript. A function is a named block of statements. The benefit that you get from using functions is that you don't have to type the statement for the function each time you want to use them. This saves time and reduces the chance of introducing errors into your application. Also, if you need to change the function, you only have to change it in one place. Some rules of functions are:
  • A function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.
  • Function names are case-insensitive.
  • A function is a block of code that can be executed whenever we need it.
  • The name of a function cannot be a global identifier.
  • The name of a function cannot be a reserved keyword of the TypeScript language.
When you call a function, you code the name of the function and a set of parentheses that contains a list of the parameters that you are passing to the function. If no parameters are required, you code an empty set of parentheses.
 
Example The following example shows a simple function without any parameters or return type. In this example, I have a simple function class and define an Armstrong function in the class. We enter minimum and maximum values for a range for getting an Armstrong number between those values. Let's use the following procedure.  
 
Step 1
Open Visual Studio 2012 and click "File" -> "New" -> "Project...". A window is shown. Give the name of your application as "simple_function" and then click ok.  
 
Step 2
After this session, the project has been created. A new window is opened on the right side. This window is called the Solution Explorer. Solution Explorer contains the ts file, js file, CSS file, and Html file.  
 

Coding

 

simple_function.ts

  1. class simplefunction {  
  2.  armstrong() {  
  3.   var min: number, max: number;  
  4.   var num: number, r: number, sum: number, temp: number;  
  5.   min = parseInt(prompt("Enter a minimum range"));  
  6.   max = parseInt(prompt("Enter a maximum range"));  
  7.   for (num = min; num <= max; num++) {  
  8.    temp = num;  
  9.    sum = 0;  
  10.    while (temp != 0) {  
  11.     r = Math.floor(temp % 10);  
  12.     sum = sum + (r * r * r);  
  13.     temp = Math.floor(temp / 10);  
  14.    }  
  15.    if (sum == num)  
  16.     alert("Armstrong number is ->" + num);  
  17.   }  
  18.  }  
  19. }  
  20. window.onload = () => {  
  21.  var greeter = new simplefunction();  
  22.  greeter.armstrong();  
  23. };  

demo.html

  1. < !DOCTYPEhtml >  
  2.  <  
  3.  htmllang = "en"  
  4. xmlns = "http://www.w3.org/1999/xhtml" >  
  5.  <  
  6.  head >  
  7.  <  
  8.  metacharset = "utf-8" / >  
  9.  <  
  10.  title > Simple Function < /title>  
  11.  <  
  12.  linkrel = "stylesheet"  
  13. href = "app.css"  
  14. type = "text/css" / >  
  15.  <  
  16.  scriptsrc = "app.js" > < /script>  
  17.  <  
  18.  /head>  
  19.  <  
  20.  body >  
  21.  <  
  22.  h2 > Simple Function in TypeScript HTML App < /h2>  
  23.  <  
  24.  divid = "content" / >  
  25.  <  
  26.  /body>  
  27.  <  
  28.  /html>  

app.js

  1. var simplefunction = (function() {  
  2.  function simplefunction() {}  
  3.  simplefunction.prototype.armstrong = function() {  
  4.   var min;  
  5.   var max;  
  6.   var num;  
  7.   var r;  
  8.   var sum;  
  9.   var temp;  
  10.   min = parseInt(prompt("Enter a minimum range"));  
  11.   max = parseInt(prompt("Enter a maximum range"));  
  12.   for (num = min; num <= max; num++) {  
  13.    temp = num;  
  14.    sum = 0;  
  15.    while (temp != 0) {  
  16.     r = Math.floor(temp % 10);  
  17.     sum = sum + (r * r * r);  
  18.     temp = Math.floor(temp / 10);  
  19.    }  
  20.    if (sum == num) {  
  21.     alert("Armstrong number is ->" + num);  
  22.    }  
  23.   }  
  24.  };  
  25.  return simplefunction;  
  26. })();  
  27. window.onload = function() {  
  28.  var el = document.getElementById('content');  
  29.  var greeter = new simplefunction();  
  30.  greeter.armstrong();  
  31. };  
Output 1
 
min-value.jpg
 
Enter a minimum value then click on Ok.
 
Output 2
 
max-value.jpg
 
Enter a maximum value then click on ok.
 
Output 3
 
armstrong-number-1.jpg
 
Output 4
 
second-armstrong-number.jpg
 
Reference By
 
http://www.typescriptlang.org/


Similar Articles