Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
ArraysTwoDim.html 1.87 KiB
<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title>Two-Dimensional Arrays</title>
</head>

<body>
    <script>
        "use strict";
        main();

        function main() {
            document.writeln("<h2>First Data Set</h2>");
            const firstArray = [
                [10, 20, 30],
                [40, 50, 60],
                [70, 80, 90]
            ];
            printArray(firstArray);

            document.writeln("<h2>Second Data Set</h2>");
            let secondArray = createArray(2, 3);
            initializeArray(secondArray, 1);
            printArray(secondArray);

            document.writeln("<h2>Third Data Set (Ragged Array)</h2>");
            const thirdArray = [
                [100, 200, 300],
                [400],
                [700, 800],
                [],
                [500, 600, 900, 1000]
            ];
            printArray(thirdArray);
        }

        function createArray(maxRows, maxCols) {
            let newArray = new Array(maxRows);

            for (let row = 0; row < maxRows; row++) {
                newArray[row] = new Array(maxCols);
            }

            return newArray;
        }

        function initializeArray(data, initialValue) {
            for (let row = 0; row < data.length; row++) {
                for (let col = 0; col < data[row].length; col++) {
                    data[row][col] = initialValue;
                    initialValue++;
                }
            }
        }

        function printArray(data) {
            for (let row = 0; row < data.length; row++) {
                for (let col = 0; col < data[row].length; col++) {
                    document.writeln(data[row][col] + "&nbsp;&nbsp;");
                }
                document.writeln("<br>");
            }
        }

    </script>
</body>

</html>