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 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 simplefunction 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

class simplefunction

{  

    armstrong()

    {

        var min: number,max:number;

        var num: number, r: number, sum: number, temp: number;

        min = parseInt(prompt("Enter a minimum range"));

        max = parseInt(prompt("Enter a maximum range"));

        for(num=min;num<=max;num++)

        {

         temp=num;

         sum = 0;

 

         while (temp != 0) {

             r = Math.floor(temp % 10);

             sum = sum + (r * r * r);

             temp = Math.floor(temp / 10);

         }

             if (sum == num)

              alert("Armstrong number is ->" + num);        

         }

    }

}

window.onload = () => {

    var greeter = new simplefunction();

    greeter.armstrong();

};

 

demo.html

<!DOCTYPEhtml>

 

<htmllang="en"xmlns="http://www.w3.org/1999/xhtml">

<head>

    <metacharset="utf-8"/>

    <title>Simple Function</title>

    <linkrel="stylesheet"href="app.css"type="text/css"/>

    <scriptsrc="app.js"></script>

</head>

<body>

    <h2>Simple Function in TypeScript HTML App</h2>

    <divid="content"/>

</body>

</html>

 

app.js

var simplefunction = (function () {

    function simplefunction() { }

    simplefunction.prototype.armstrong = function () {

        var min;

        var max;

 

        var num;

        var r;

        var sum;

        var temp;

 

        min = parseInt(prompt("Enter a minimum range"));

        max = parseInt(prompt("Enter a maximum range"));

        for(num = min; num <= max; num++) {

            temp = num;

            sum = 0;

            while(temp != 0) {

                r = Math.floor(temp % 10);

                sum = sum + (r * r * r);

                temp = Math.floor(temp / 10);

            }

            if(sum == num) {

                alert("Armstrong number is ->" + num);

            }

        }

    };

    return simplefunction;

})();

window.onload = function () {

    var el = document.getElementById('content');

    var greeter = new simplefunction();

    greeter.armstrong();

};

 

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/

Up Next
    Ebook Download
    View all

    Test

    Read by 16 people
    Download Now!
    Learn
    View all