How To Use Simple Array in TypeScript

Simple Array in TypeScript

An array is an object that contains one or more items called elements. Each of these elements can be a primitive data type or an object. For instance, you can store numbers, strings, and Date objects in the same array. The length of an array is the number of elements in the array. If you create an array without specifying the length, the array doesn't contain any elements. When you create an array of one or more elements without assigning values to them, each element is set to undefined. If you use an index then 0 is the first element, 1 is the second element, and so on.

Example

The following example describes how to use an array in TypeScript. The following code implements a linear search to determine whether a given number is present in an array and if it is present then at what location it is. It is very simple and works as follows.

Step 1

Open Visual Studio 2012 and click on "File" menu -> "New" -> "Project". After that, a window is opened; enter the name of your applicatio,n like "Linear_Search", then click on the Ok button.

Step 2

After Step 1 your project has been created. The Solution Explorer, which is at the right side of your project, contains the js file, ts file, css file and html files.

Step 3

Linear_Search.ts

class LinearSeach

{

    Searching()

    {

        var search: number, c: number, num: number;

        var array: number[] = [100];

        num = parseInt(prompt("Enter the number of elements in Array"));

        for (c = 0; c < num; c++)

        {

            array[c] = parseInt(prompt("Enter "+ num +" Number"));        

        }

        var x = 0;

        for (x = 0; x < num; x++)

        {

            var span = document.createElement("span");

            span.innerText = "Enter number "+x + "is "+ array[x] + "\n";

            document.body.appendChild(span);

        }

        search = parseInt(prompt("Enter the number to search \n"));

        for (c = 0; c < num; c++)

        {

            if (array[c] == search)

            {

                var n;

                n = c + 1;

                alert(search + " is present at location ->"+ n);

                break;

            }

        }

        if(c==num)

            alert("Number is not present in array -> " + search);

    }

}

window.onload = () => {

    var greeter = new LinearSeach();

    greeter.Searching();

};


LinearSearch.html

 

<!DOCTYPEhtml>

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

<head>

    <metacharset="utf-8"/>

    <title>Simple Array</title>

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

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

</head>

<body>

    <h1>Simple Array in TypeScript</h1>

    <h2style="color:#4db8e8">Linear Search in TypeScript</h2>

    <divid="content"/>

</body>

</html>


app.js

 

var LinearSeach = (function () {

    function LinearSeach() { }

    LinearSeach.prototype.Searching = function () {

        var search;

        var c;

        var num;

 

        var array = [

            100

        ];

        num = parseInt(prompt("Enter the number of elements in Array"));

        for(c = 0; c < num; c++) {

            array[c] = parseInt(prompt("Enter " + num + " Number"));

        }

        var x = 0;

        for(x = 0; x < num; x++) {

            var span = document.createElement("span");

            span.innerText = "Enter number " + x + "is " + array[x] + "\n";

            document.body.appendChild(span);

        }

        search = parseInt(prompt("Enter the number to search \n"));

        for(c = 0; c < num; c++) {

            if(array[c] == search) {

                var n;

                n = c + 1;

                alert(search + " is present at location ->" + n);

                break;

            }

        }

        if(c == num) {

            alert("Number is not present in array -> " + search);

        }

    };

    return LinearSeach;

})();

window.onload = function () {

    var greeter = new LinearSeach();

    greeter.Searching();

};

 

Output 1

Enter the number of Elements and then click on ok, as in:


Linear-image1-in-type-script.jpg

 

Output 2

Enter numbers.


Linear-image2-in-type-script.jpg


Linear-image3-in-type-script.jpg


Linear-image4-in-type-script.jpg


Linear-image5-in-type-script.jpg

Linear-image6-in-type-script.jpg

Output 3

Then enter the number to search for:


Linear-image7-in-type-script.jpg


Output 4

If the specified number is present in the array then the location that it occurs at is shown:


Linear-image8-in-type-script.jpg


Output 5

And if the specified number is not present in the array then:


Linear-image9-in-type-script.jpg


Linear-image10-in-type-script.jpg

 

Up Next
    Ebook Download
    View all

    Test

    Read by 16 people
    Download Now!
    Learn
    View all