Arrays

>
Declaring Arrays
    <!DOCTYPE html>
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <title>Declaring Arrays</title>
        <script>
            window.onload = function () {
                // one way to declare an array
                var teams = ["Yankees", "Mets", "Giants", "Jets", "Knicks", "Nets", "Islanders", "Rangers"];
                console.log(teams);
                // formal declaration of an array
                teams = new Array("Yankees", "Mets", "Giants", "Jets", "Knicks", "Nets", "Islanders", "Rangers");
                console.log(teams);
          }
        </script>
    </head>
    <body>

    </body>
    </html>
        
Top

Index

Accessing and Editing Array Elements
    <!DOCTYPE html>
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <title>Accessing and Editing Arrays</title>
        <script>
            window.onload = function () {
                // one way to declare an array
                var teams = ["Yankees", "Mets", "Giants", "Jets", "Knicks", "Nets", "Islanders", "Rangers"];
                console.log(teams);

                var resultDiv = document.getElementById('result');
                resultDiv.innerHTML = teams[3];
                resultDiv.innerHTML += "<br/>" + teams[0];
                resultDiv.innerHTML += "<br/>" + teams[5];
                // will show 'undefined'
                resultDiv.innerHTML += "<br/>" + teams[10];

                teams[0] = "Red Sox";
                console.log(teams);
          }
        </script>
    </head>
    <body>
        <div id="result"></div>
    </body>
    </html>
        
Top

Index

Looping Through Arrays
    <!DOCTYPE html>
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <title>Looping Through Arrays</title>
        <script>
            window.onload = function () {
                var teams = ["Yankees", "Mets", "Giants", "Jets", "Knicks", "Nets", "Islanders", "Rangers"];
                console.log(teams.length);
                var x = 0;
                var out = "";
                while (x < teams.length) {
                    out += "<br/>" + teams[x];
                    x++;
                }
                document.getElementById('result').innerHTML = out;
          }
        </script>
    </head>
    <body>
        <div id="result"></div>
    </body>
    </html>
        
Top

Index

Array Functions
    <!DOCTYPE html>
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <title>
            Array Functions
        </title>
        <script>
            var resultDiv;
            window.onload = function () {
                resultDiv = document.getElementById('result');
                var teams = ["Yankees", "Mets", "Giants", "Jets", "Knicks", "Nets", "Islanders", "Rangers"];
                teams.forEach(myFunction);

                console.log(teams.indexOf("Mets"));
                // Red Sox not in array so -1 is logged
                console.log(teams.indexOf("Red Sox"));
                // add element to end of array
                teams.push("Red Sox");
                // Red Sox added to array so 8 is logged
                console.log(teams.indexOf("Red Sox"));
                // remove last element of array
                teams.pop();

                resultDiv.innerHTML += "<p/>";
                teams.sort().forEach(myFunction);
            }
            function myFunction(item, index) {
                resultDiv.innerHTML += index + "|" + item + "<br/>";
            }
        </script>
    </head>
    <body>
        <div id="result"></div>
    </body>
    </html>
        
Top

Index

n4jvp.com